SQL Reference > Type Functions

Type Functions

This page covers type functions in Rockset. For more on Rockset types, check out the data types page.

To cast values from one type to another, you can use one of four cast functions: CAST, TRY_CAST, STATIC_CAST and TRY_STATIC_CAST.

CAST

CAST(x AS type)

Lexical cast that, in addition to STATIC_CAST, supports casting between string and other scalar types. Errors the query if the cast is not supported.

> SELECT CAST('foo' AS int)
Error
> SELECT CAST(true AS int)
1
> SELECT CAST('2' AS int)
2

Supported lexical casts are shown in the table below.

From typeTo type
intfloat
intbool
intstring
boolint
boolfloat
boolstring
floatint
floatbool
floatstring
stringint
stringfloat
stringbool
stringbytes
stringdate
stringtime
stringdatetime
stringtimestamp
bytesstring
datestring
timestring
datetimestring
timestampstring

IS_SCALAR

IS_SCALAR(x)

Returns true if x is a scalar, which is any type except array, object, and null.

STATIC_CAST

STATIC_CAST(x AS type)

Static cast that converts values from one data type to another. Supports casts between numeric types and casts to bool with Python semantics. Errors the query if the cast is not supported.

> SELECT STATIC_CAST(true AS int)
1
> SELECT STATIC_CAST('2' AS int)
Error

Supported static casts are show below.

From typeTo type
intfloat
intbool
boolint
boolfloat
floatint
floatbool
stringbytes
bytesbool
bytesstring

TRY_CAST

TRY_CAST(x AS type)

Same as CAST, except it doesn’t error out the query for an unsupported cast; a null is returned instead.

> SELECT TRY_CAST('foo' AS int)
null

TRY_STATIC_CAST

TRY_STATIC_CAST(x AS type)

Same as STATIC_CAST, except it doesn’t error out the query for an unsupported cast; a null is returned instead.

> SELECT TRY_STATIC_CAST('2' AS int)
null

TYPEOF

TYPEOF(x)

Returns a string, name of the type of x. See the page on data types for a list of type names.