Skip to content

Commit 6d867db

Browse files
soyeric128Chasen-Zhang
authored andcommitted
TO_BINARY
1 parent 611a70c commit 6d867db

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: TO_BINARY
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.673"/>
7+
8+
Converts supported data types, including variant, bitmap, geometry, and geography, into their binary representation.
9+
10+
See also: [TRY_TO_BINARY](try-to-binary.md)
11+
12+
## Syntax
13+
14+
```sql
15+
TO_BINARY( <expr> )
16+
```
17+
18+
## Examples
19+
20+
This example converts JSON data to binary:
21+
22+
```sql
23+
SELECT TO_BINARY(PARSE_JSON('{"key":"value", "number":123}')) AS binary_variant;
24+
25+
┌──────────────────────────────────────────────────────────────────────────┐
26+
│ binary_variant │
27+
├──────────────────────────────────────────────────────────────────────────┤
28+
│ 40000002100000031000000610000005200000026B65796E756D62657276616C7565507B │
29+
└──────────────────────────────────────────────────────────────────────────┘
30+
```
31+
32+
This example converts bitmap data to binary:
33+
34+
```sql
35+
SELECT TO_BINARY(TO_BITMAP('10,20,30')) AS binary_bitmap;
36+
37+
┌──────────────────────────────────────────────────────────────────────┐
38+
│ binary_bitmap │
39+
├──────────────────────────────────────────────────────────────────────┤
40+
│ 0100000000000000000000003A3000000100000000000200100000000A0014001E00 │
41+
└──────────────────────────────────────────────────────────────────────┘
42+
```
43+
44+
This example converts geometry data (WKT format) to binary:
45+
46+
```sql
47+
SELECT TO_BINARY(ST_GEOMETRYFROMWKT('SRID=4326;POINT(1.0 2.0)')) AS binary_geometry;
48+
49+
┌────────────────────────────────────────────────────┐
50+
│ binary_geometry │
51+
├────────────────────────────────────────────────────┤
52+
│ 0101000020E6100000000000000000F03F0000000000000040 │
53+
└────────────────────────────────────────────────────┘
54+
```
55+
56+
This example converts geography data (EWKT format) to binary:
57+
58+
```sql
59+
SELECT TO_BINARY(ST_GEOGRAPHYFROMEWKT('SRID=4326;POINT(-122.35 37.55)')) AS binary_geography;
60+
61+
┌────────────────────────────────────────────────────┐
62+
│ binary_geography │
63+
├────────────────────────────────────────────────────┤
64+
│ 0101000020E61000006666666666965EC06666666666C64240 │
65+
└────────────────────────────────────────────────────┘
66+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: TRY_TO_BINARY
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.673"/>
7+
8+
Attempts to convert supported data types, including variant, bitmap, geometry, and geography, into their binary representation, returning `NULL` if the conversion fails.
9+
10+
See also: [TO_BINARY](to-binary.md)
11+
12+
## Syntax
13+
14+
```sql
15+
TRY_TO_BINARY( <expr> )
16+
```
17+
18+
## Examples
19+
20+
This example successfully converts the JSON data to binary:
21+
22+
```sql
23+
SELECT TRY_TO_BINARY(PARSE_JSON('{"key":"value", "number":123}')) AS binary_variant_success;
24+
25+
┌──────────────────────────────────────────────────────────────────────────┐
26+
│ binary_variant │
27+
├──────────────────────────────────────────────────────────────────────────┤
28+
│ 40000002100000031000000610000005200000026B65796E756D62657276616C7565507B │
29+
└──────────────────────────────────────────────────────────────────────────┘
30+
```
31+
32+
This example demonstrates that the function fails to convert when the input is `NULL`:
33+
34+
```sql
35+
SELECT TRY_TO_BINARY(PARSE_JSON(NULL)) AS binary_variant_invalid_json;
36+
37+
┌─────────────────────────────┐
38+
│ binary_variant_invalid_json │
39+
├─────────────────────────────┤
40+
NULL
41+
└─────────────────────────────┘
42+
```

0 commit comments

Comments
 (0)