|
| 1 | +========================= |
| 2 | +Type Conversion Functions |
| 3 | +========================= |
| 4 | + |
| 5 | +.. rubric:: Table of contents |
| 6 | + |
| 7 | +.. contents:: |
| 8 | + :local: |
| 9 | + :depth: 1 |
| 10 | + |
| 11 | +CAST |
| 12 | +---- |
| 13 | + |
| 14 | +Description |
| 15 | +>>>>>>>>>>> |
| 16 | + |
| 17 | +Usage: cast(expr as dateType) cast the expr to dataType. return the value of dataType. The following conversion rules are used: |
| 18 | + |
| 19 | ++------------+--------+--------+---------+-------------+--------+--------+ |
| 20 | +| Src/Target | STRING | NUMBER | BOOLEAN | TIMESTAMP | DATE | TIME | |
| 21 | ++------------+--------+--------+---------+-------------+--------+--------+ |
| 22 | +| STRING | | Note1 | Note1 | TIMESTAMP() | DATE() | TIME() | |
| 23 | ++------------+--------+--------+---------+-------------+--------+--------+ |
| 24 | +| NUMBER | Note1 | | v!=0 | N/A | N/A | N/A | |
| 25 | ++------------+--------+--------+---------+-------------+--------+--------+ |
| 26 | +| BOOLEAN | Note1 | v?1:0 | | N/A | N/A | N/A | |
| 27 | ++------------+--------+--------+---------+-------------+--------+--------+ |
| 28 | +| TIMESTAMP | Note1 | N/A | N/A | | DATE() | TIME() | |
| 29 | ++------------+--------+--------+---------+-------------+--------+--------+ |
| 30 | +| DATE | Note1 | N/A | N/A | N/A | | N/A | |
| 31 | ++------------+--------+--------+---------+-------------+--------+--------+ |
| 32 | +| TIME | Note1 | N/A | N/A | N/A | N/A | | |
| 33 | ++------------+--------+--------+---------+-------------+--------+--------+ |
| 34 | + |
| 35 | +Note1: the conversion follow the JDK specification. |
| 36 | + |
| 37 | +Cast to string example:: |
| 38 | + |
| 39 | + os> source=people | eval `cbool` = CAST(true as string), `cint` = CAST(1 as string), `cdate` = CAST(CAST('2012-08-07' as date) as string) | fields `cbool`, `cint`, `cdate` |
| 40 | + fetched rows / total rows = 1/1 |
| 41 | + +---------+--------+------------+ |
| 42 | + | cbool | cint | cdate | |
| 43 | + |---------+--------+------------| |
| 44 | + | true | 1 | 2012-08-07 | |
| 45 | + +---------+--------+------------+ |
| 46 | + |
| 47 | +Cast to number example:: |
| 48 | + |
| 49 | + os> source=people | eval `cbool` = CAST(true as int), `cstring` = CAST('1' as int) | fields `cbool`, `cstring` |
| 50 | + fetched rows / total rows = 1/1 |
| 51 | + +---------+-----------+ |
| 52 | + | cbool | cstring | |
| 53 | + |---------+-----------| |
| 54 | + | 1 | 1 | |
| 55 | + +---------+-----------+ |
| 56 | + |
| 57 | +Cast to date example:: |
| 58 | + |
| 59 | + os> source=people | eval `cdate` = CAST('2012-08-07' as date), `ctime` = CAST('01:01:01' as time), `ctimestamp` = CAST('2012-08-07 01:01:01' as timestamp) | fields `cdate`, `ctime`, `ctimestamp` |
| 60 | + fetched rows / total rows = 1/1 |
| 61 | + +------------+----------+---------------------+ |
| 62 | + | cdate | ctime | ctimestamp | |
| 63 | + |------------+----------+---------------------| |
| 64 | + | 2012-08-07 | 01:01:01 | 2012-08-07 01:01:01 | |
| 65 | + +------------+----------+---------------------+ |
| 66 | + |
| 67 | +Cast function can be chained:: |
| 68 | + |
| 69 | + os> source=people | eval `cbool` = CAST(CAST(true as string) as boolean) | fields `cbool` |
| 70 | + fetched rows / total rows = 1/1 |
| 71 | + +---------+ |
| 72 | + | cbool | |
| 73 | + |---------| |
| 74 | + | True | |
| 75 | + +---------+ |
0 commit comments