|
| 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 | +``` |
0 commit comments