Data Type Conversion
Data type conversions can be achieved through either data type conversion functions or function cast($).
DolphinDB supports type conversion functions including string, bool, char, short, int, long, double, date, month, time, second, minute, datetime, timestamp, symbol, nanotime, nanotimestamp, datehour, uuid, ipaddr, int128, blob, complex, point, duration, decimal32, decimal64, decimal128.
Each individual function has 3 usages:
- 
                Create a new variable with null value 
- 
                Convert from a string 
- 
                Convert from other data types 
- 
                    All these functions (except for symbol) accept zero or one parameter. If there is no input parameter, it creates a corresponding scalar object with a default value. If the parameter is a string or string vector, it will convert the string to the target data type accordingly. Other types are converted accordingly if they are semantically compatible.
- 
                    The short, int, and long functions use rounding to convert floating-point numbers to integers, and use truncation to convert strings by discarding the fractional part of that given value. 
string
// make a new string with default value ""
string()=="";
// output: 1
string(10);
// output: 10
typestr string(108.5);
// output: STRING
string(now());
// output: 2016.03.02T20:55:31.287bool
x=bool();
x;
// output: 00b
typestr x;
// output: BOOL
bool(`true);
// output
1
bool(`false);
// output: 0
bool(100.2);
// output: 1
bool(0);
// output: 0decimal32
a=decimal32(142, 2)
a
// output: 142.00
b=decimal32(1\7, 6)
b
// output: 0.142857
a+b
// output: 142.142857
a*b
// output: 20.28569400
decimal32("3.1415926535", 4)
// output: 3.1415All elements in a DECIMAL-type vector must have the same data type and scale. For example:
d1=[1.23$DECIMAL32(4), 3$DECIMAL32(4), 3.14$DECIMAL32(4)];
// output: [1.2300,3.0000,3.1400]
typestr(d1);
// output: FAST DECIMAL32 VECTOR
//If the elements have different scales, the sever will create and ouput a tuple.
d2=[1.23$DECIMAL32(4), 3$DECIMAL32(4), 3.14$DECIMAL32(3)];
// output: (1.2300,3.0000,3.140)
typestr(d2);
// output: ANY VECTORWhen converting data of STRING or SYMBOL types to DECIMAL, the behavior differs depending on the server version.
- For versions earlier than 2.00.10, the extra digits exceeding the scale will be
                    directly
                    truncated.symbol(["1.341", "4.5677"])$DECIMAL32(2) // output: [1.34,4.56]
- For versions 2.00.10 and later, the value will be rounded to the
                    nearest valid decimal based on the specified
                    scale.symbol(["1.341", "4.5677"])$DECIMAL32(2) // output: [1.34,4.57]
decimal64
a=decimal64(142, 2)
a
// output: 142.00
b=decimal64(1\7, 6)
b
// output: 0.142857
a+b
// output: 142.142857
a*b
// output: 20.28569400
decimal64("3.1415926535", 4)
// output: 3.1415All elements in a DECIMAL-type vector must have the same data type and scale. For example:
d1=[1.23$DECIMAL64(4), 3$DECIMAL64(4), 3.14$DECIMAL64(4)];
// output: [1.2300,3.0000,3.1400]
typestr(d1);
// output: FAST DECIMAL64 VECTOR
//If the elements have different scales, the sever will create and ouput a tuple.
d2=[1.23$DECIMAL64(4), 3$DECIMAL64(4), 3.14$DECIMAL64(3)];
// output: (1.2300,3.0000,3.140)
typestr(d2);
// output: ANY VECTORWhen converting data of STRING or SYMBOL types to DECIMAL, the behavior differs depending on the server version.
- For versions earlier than 2.00.10, the extra digits exceeding the scale will be
                    directly
                    truncated.symbol(["1.341", "4.5677"])$DECIMAL64(2) // output: [1.34,4.56]
- For versions 2.00.10 and later, the value will be rounded to the
                    nearest valid decimal based on the specified
                    scale.symbol(["1.341", "4.5677"])$DECIMAL64(2) // output: [1.34,4.57]
decimal128
a=decimal128(142, 2)
a
// output: 142.00
b=decimal128(1\7, 6)
b
// output: 0.142857
a+b
// output: 142.142857
a*b
// output: 20.28569400
decimal128("3.1415926535", 4)
// output: 3.1416All elements in a DECIMAL-type vector must have the same data type and scale. For example:
d1=[1.23$DECIMAL128(4), 3$DECIMAL128(4), 3.14$DECIMAL128(4)]; 
// output: [1.2300,3.0000,3.1400]
typestr(d1);
// output: FAST DECIMAL128 VECTOR
//If the elements have different scales, the sever will create and ouput a tuple.
d2=[1.23$DECIMAL128(4), 3$DECIMAL128(4), 3.14$DECIMAL128(3)];
// output: (1.2300,3.0000,3.140)
typestr(d2);
// output: ANY VECTORint
x=int();
x;
// output: 00i
typestr x;
// output: INT
int(`10.9);
// output: 10
int(2147483647);
// output: 2147483647
// maximum value for an INT is 2^31-1=2147483647
int(2147483648);
// output: 00ishort
x=short();
x;
// output:00h
typestr x;
// output: SHORT
short(`12.3);
// output: 12
short(`120.9c);
// output: 120
short(32767);
// output: 32767
/ maximum value for a SHORT is 2^15-1=32767
short(32768);
// output: 00hlong
x=long();
x;
// output: 00l
typestr x;
// output: LONG
long(`10.9);
// output: 10
long(9223372036854775807l);
// output: 9223372036854775807
// maximum value for LONG is 2^63-1=9223372036854775807
long(9223372036854775808l);
// output: 9223372036854775807char
x=char();
x;
// output: 00c
typestr x;
// output: CHAR
a=char(99);
a;
// output: 'c'
typestr a;
// output: CHAR
char(a+5);
// output: 'h'double
x=double();
x;
// output: 00F
typestr x;
// output: DOUBLE
typestr double(`10);
// output: DOUBLE
double(`10.9);
// output: 10.9
double(now());
// output: 5.297834e+011date
date();
// output: 00d
date(`2011.10.12);
// output: 2011.10.12
date(now());
// output: 2016.03.02datehour
datehour(1)
// output: 1970.01.01T01
datehour(2012.06.13 13:30:10);
// output: 2012.06.13T13
datehour([2012.06.15 15:32:10.158,2012.06.15 17:30:10.008]);
// output: [2012.06.15T15,2012.06.15T17]
datehour(2012.01M)
// output: 2012.01.01T00datetime
datetime(2009.11.10);
// output: 2009.11.10T00:00:00
typestr datetime(2009.11.10);
// output: DATETIME
datetime(now());
// output: 2016.03.02T20:51:10timestamp
timestamp(2016.10.12);
// output: 2016.10.12T00:00:00.000
timestamp(2016.10.12)+1;
// output: 2016.10.12T00:00:00.001
timestamp(now());
// output: 2016.10.13T20:28:45.104month
 month();
// output: 0M
 month(`2012.12m);
// output: 012.12M
/make a month variable from date
 month(2012.12.23);
// output: 012.12M
//make a month variable from timestamp
 month(now());
// output: 016.03Msecond
second();
// output: 00s
second("19:36:12");
// output: 19:36:12
second(now());
// output: 20:50:31minute
minute();
// output: 00m
minute(now());
// output: 20:49mhour
hour(2012.12.03 01:22:01);
// output: 1time
time();
// output: 00t
time("12:32:56.356");
// output: 12:32:56.356
time(now());
// output: 20:49:12.564symbol
x=`AMZN`AAPL`GOOG`FB`SNAP;
x;
// output: ["AMZN","AAPL","GOOG","FB","SNAP"]
typestr x;
// output: STRING VECTOR
y=symbol(x);
y;
// output: ["AMZN","AAPL","GOOG","FB","SNAP"]
typestr y;
// output: FAST SYMBOL VECTORnanotime
nanotime(1000000000);
// output: 00:00:01.000000000
nanotime(12:06:09 13:08:01);
// output: [12:06:09.000000000,13:08:01.000000000]
nanotime(2012.12.03 01:22:01.123456789);
// output: 01:22:01.123456789
nanotime('13:30:10.008007006');
// output: 13:30:10.008007006nanotimestamp
nanotimestamp(1);
// output: 1970.01.01 00:00:00.000000001
nanotimestamp(1000000000);
// output: 1970.01.01 00:00:01.000000000
nanotimestamp(2012.12.03 12:06:09 2012.12.03 13:08:01);
// output: [2012.12.03 12:06:09.000000000,2012.12.03 13:08:01.000000000]
nanotimestamp(2012.12.03 01:22:01.123456789);
// output: 2012.12.03 01:22:01.123456789
nanotimestamp('2012.12.03 13:30:10.008007006');
// output: 2012.12.03 13:30:10.008007006
nanotimestamp(now());
// output: 2024.02.22 16:14:28.627000000
nanotimestamp(2012.01M)
// output: 2012.01.01T00:00:00.000000000
uuid
uuid("");
// output: 00000000-0000-0000-0000-000000000000
a=uuid("9d457e79-1bed-d6c2-3612-b0d31c1881f6");
a;
// output: 9d457e79-1bed-d6c2-3612-b0d31c1881f6
typestr(a);
// output: UUIDipaddr
a=ipaddr("192.168.1.13");
a;
// output: 192.168.1.13
typestr(a);
// output: IPADDRint128
a=int128("e1671797c52e15f763380b45e841ec32")
// output: e1671797c52e15f763380b45e841ec32
typestr(a);
// output: INT128duration
y=duration("20H")
y
// output: 20H
typestr(y)
// output: DURATION
duration("3XNYS")
// output: 3XNYSWhen the unit of time is not specified, the system defaults it as seconds (s):
t=table(take(2018.01.01T01:00:00+1..10,10) join take(2018.01.01T02:00:00+1..10,10) join take(2018.01.01T08:00:00+1..10,10) as time, rand(1.0, 30) as x);
select max(x) from t group by bar(time, 5);| bar_time | max_x | 
|---|---|
| 2018.01.01T01:00:00 | 0.8824 | 
| 2018.01.01T01:00:05 | 0.8027 | 
| 2018.01.01T01:00:10 | 0.572 | 
| 2018.01.01T02:00:00 | 0.8875 | 
| 2018.01.01T02:00:05 | 0.8542 | 
| 2018.01.01T02:00:10 | 0.4287 | 
| 2018.01.01T08:00:00 | 0.9294 | 
| 2018.01.01T08:00:05 | 0.9804 | 
| 2018.01.01T08:00:10 | 0.2147 | 
Specify the unit of time as minute (m) and group time column with 1-minute intervals:
select max(x) from t group by bar(time, 1m);| bar_time | max_x | 
|---|---|
| 2018.01.01T01:00:00 | 0.8824 | 
| 2018.01.01T02:00:00 | 0.8875 | 
| 2018.01.01T08:00:00 | 0.9804 | 
cast(X, dataTypeName) / $
x=3.1;
typestr x;
// output: DOUBLE
x=cast(x, int);
// can also use cast(x, INT)
x;
// output: 3
typestr x;
// output: INT
19.99$INT;
// output: 20
syms =`IBM`C`MS`MSFT`JPM`ORCL`BIDU`SOHU
typestr syms;
// output: STRING VECTOR
syms=syms$SYMBOL;
typestr syms;
// output: FAST SYMBOL VECTORFor more about the cast function please see cast.
