Type Conversion Functions

Common Issues of Numeric Conversions

When you convert a value from one to another data type, you should remember that in common case, it is an unsafe operation that can lead to a data loss. A data loss can occur if you try to fit value from a larger data type to a smaller data type, or if you convert values between different data types.

ClickHouse has the same behavior as C++ programs.

toInt(8|16|32|64|128|256)

Converts an input value to the Int data type. This function family includes:

  • toInt8(expr) — Results in the Int8 data type.
  • toInt16(expr) — Results in the Int16 data type.
  • toInt32(expr) — Results in the Int32 data type.
  • toInt64(expr) — Results in the Int64 data type.
  • toInt128(expr) — Results in the Int128 data type.
  • toInt256(expr) — Results in the Int256 data type.

Arguments

  • exprExpression returning a number or a string with the decimal representation of a number. Binary, octal, and hexadecimal representations of numbers are not supported. Leading zeroes are stripped.

Returned value

Integer value in the Int8, Int16, Int32, Int64, Int128 or Int256 data type.

Functions use rounding towards zero, meaning they truncate fractional digits of numbers.

The behavior of functions for the NaN and Inf arguments is undefined. Remember about numeric convertions issues, when using the functions.

Example

Query:

  1. SELECT toInt64(nan), toInt32(32), toInt16('16'), toInt8(8.8);

Result:

  1. ┌─────────toInt64(nan)─┬─toInt32(32)─┬─toInt16('16')─┬─toInt8(8.8)─┐
  2. -9223372036854775808 32 16 8
  3. └──────────────────────┴─────────────┴───────────────┴─────────────┘

toInt(8|16|32|64|128|256)OrZero

It takes an argument of type String and tries to parse it into Int (8 | 16 | 32 | 64 | 128 | 256). If failed, returns 0.

Example

Query:

  1. SELECT toInt64OrZero('123123'), toInt8OrZero('123qwe123');

Result:

  1. ┌─toInt64OrZero('123123')─┬─toInt8OrZero('123qwe123')─┐
  2. 123123 0
  3. └─────────────────────────┴───────────────────────────┘

toInt(8|16|32|64|128|256)OrNull

It takes an argument of type String and tries to parse it into Int (8 | 16 | 32 | 64 | 128 | 256). If failed, returns NULL.

Example

Query:

  1. SELECT toInt64OrNull('123123'), toInt8OrNull('123qwe123');

Result:

  1. ┌─toInt64OrNull('123123')─┬─toInt8OrNull('123qwe123')─┐
  2. 123123 ᴺᵁᴸᴸ
  3. └─────────────────────────┴───────────────────────────┘

toUInt(8|16|32|64|256)

Converts an input value to the UInt data type. This function family includes:

  • toUInt8(expr) — Results in the UInt8 data type.
  • toUInt16(expr) — Results in the UInt16 data type.
  • toUInt32(expr) — Results in the UInt32 data type.
  • toUInt64(expr) — Results in the UInt64 data type.
  • toUInt256(expr) — Results in the UInt256 data type.

Arguments

  • exprExpression returning a number or a string with the decimal representation of a number. Binary, octal, and hexadecimal representations of numbers are not supported. Leading zeroes are stripped.

Returned value

Integer value in the UInt8, UInt16, UInt32, UInt64 or UInt256 data type.

Functions use rounding towards zero, meaning they truncate fractional digits of numbers.

The behavior of functions for negative agruments and for the NaN and Inf arguments is undefined. If you pass a string with a negative number, for example '-32', ClickHouse raises an exception. Remember about numeric convertions issues, when using the functions.

Example

Query:

  1. SELECT toUInt64(nan), toUInt32(-32), toUInt16('16'), toUInt8(8.8);

Result:

  1. ┌───────toUInt64(nan)─┬─toUInt32(-32)─┬─toUInt16('16')─┬─toUInt8(8.8)─┐
  2. 9223372036854775808 4294967264 16 8
  3. └─────────────────────┴───────────────┴────────────────┴──────────────┘

toUInt(8|16|32|64|256)OrZero

toUInt(8|16|32|64|256)OrNull

toFloat(32|64)

toFloat(32|64)OrZero

toFloat(32|64)OrNull

toDate

Alias: DATE.

toDateOrZero

toDateOrNull

toDateTime

toDateTimeOrZero

toDateTimeOrNull

toDecimal(32|64|128|256)

Converts value to the Decimal data type with precision of S. The value can be a number or a string. The S (scale) parameter specifies the number of decimal places.

  • toDecimal32(value, S)
  • toDecimal64(value, S)
  • toDecimal128(value, S)
  • toDecimal256(value, S)

toDecimal(32|64|128|256)OrNull

Converts an input string to a Nullable(Decimal(P,S)) data type value. This family of functions include:

  • toDecimal32OrNull(expr, S) — Results in Nullable(Decimal32(S)) data type.
  • toDecimal64OrNull(expr, S) — Results in Nullable(Decimal64(S)) data type.
  • toDecimal128OrNull(expr, S) — Results in Nullable(Decimal128(S)) data type.
  • toDecimal256OrNull(expr, S) — Results in Nullable(Decimal256(S)) data type.

These functions should be used instead of toDecimal*() functions, if you prefer to get a NULL value instead of an exception in the event of an input value parsing error.

Arguments

  • exprExpression, returns a value in the String data type. ClickHouse expects the textual representation of the decimal number. For example, '1.111'.
  • S — Scale, the number of decimal places in the resulting value.

Returned value

A value in the Nullable(Decimal(P,S)) data type. The value contains:

  • Number with S decimal places, if ClickHouse interprets the input string as a number.
  • NULL, if ClickHouse can’t interpret the input string as a number or if the input number contains more than S decimal places.

Examples

Query:

  1. SELECT toDecimal32OrNull(toString(-1.111), 5) AS val, toTypeName(val);

Result:

  1. ┌──────val─┬─toTypeName(toDecimal32OrNull(toString(-1.111), 5))─┐
  2. -1.11100 Nullable(Decimal(9, 5))
  3. └──────────┴────────────────────────────────────────────────────┘

Query:

  1. SELECT toDecimal32OrNull(toString(-1.111), 2) AS val, toTypeName(val);

Result:

  1. ┌──val─┬─toTypeName(toDecimal32OrNull(toString(-1.111), 2))─┐
  2. ᴺᵁᴸᴸ Nullable(Decimal(9, 2))
  3. └──────┴────────────────────────────────────────────────────┘

toDecimal(32|64|128|256)OrZero

Converts an input value to the Decimal(P,S) data type. This family of functions include:

  • toDecimal32OrZero( expr, S) — Results in Decimal32(S) data type.
  • toDecimal64OrZero( expr, S) — Results in Decimal64(S) data type.
  • toDecimal128OrZero( expr, S) — Results in Decimal128(S) data type.
  • toDecimal256OrZero( expr, S) — Results in Decimal256(S) data type.

These functions should be used instead of toDecimal*() functions, if you prefer to get a 0 value instead of an exception in the event of an input value parsing error.

Arguments

  • exprExpression, returns a value in the String data type. ClickHouse expects the textual representation of the decimal number. For example, '1.111'.
  • S — Scale, the number of decimal places in the resulting value.

Returned value

A value in the Nullable(Decimal(P,S)) data type. The value contains:

  • Number with S decimal places, if ClickHouse interprets the input string as a number.
  • 0 with S decimal places, if ClickHouse can’t interpret the input string as a number or if the input number contains more than S decimal places.

Example

Query:

  1. SELECT toDecimal32OrZero(toString(-1.111), 5) AS val, toTypeName(val);

Result:

  1. ┌──────val─┬─toTypeName(toDecimal32OrZero(toString(-1.111), 5))─┐
  2. -1.11100 Decimal(9, 5)
  3. └──────────┴────────────────────────────────────────────────────┘

Query:

  1. SELECT toDecimal32OrZero(toString(-1.111), 2) AS val, toTypeName(val);

Result:

  1. ┌──val─┬─toTypeName(toDecimal32OrZero(toString(-1.111), 2))─┐
  2. 0.00 Decimal(9, 2)
  3. └──────┴────────────────────────────────────────────────────┘

toString

Functions for converting between numbers, strings (but not fixed strings), dates, and dates with times.
All these functions accept one argument.

When converting to or from a string, the value is formatted or parsed using the same rules as for the TabSeparated format (and almost all other text formats). If the string can’t be parsed, an exception is thrown and the request is canceled.

When converting dates to numbers or vice versa, the date corresponds to the number of days since the beginning of the Unix epoch.
When converting dates with times to numbers or vice versa, the date with time corresponds to the number of seconds since the beginning of the Unix epoch.

The date and date-with-time formats for the toDate/toDateTime functions are defined as follows:

  1. YYYY-MM-DD
  2. YYYY-MM-DD hh:mm:ss

As an exception, if converting from UInt32, Int32, UInt64, or Int64 numeric types to Date, and if the number is greater than or equal to 65536, the number is interpreted as a Unix timestamp (and not as the number of days) and is rounded to the date. This allows support for the common occurrence of writing ‘toDate(unix_timestamp)’, which otherwise would be an error and would require writing the more cumbersome ‘toDate(toDateTime(unix_timestamp))’.

Conversion between a date and date with time is performed the natural way: by adding a null time or dropping the time.

Conversion between numeric types uses the same rules as assignments between different numeric types in C++.

Additionally, the toString function of the DateTime argument can take a second String argument containing the name of the time zone. Example: Asia/Yekaterinburg In this case, the time is formatted according to the specified time zone.

Example

Query:

  1. SELECT
  2. now() AS now_local,
  3. toString(now(), 'Asia/Yekaterinburg') AS now_yekat;

Result:

  1. ┌───────────now_local─┬─now_yekat───────────┐
  2. 2016-06-15 00:11:21 2016-06-15 02:11:21
  3. └─────────────────────┴─────────────────────┘

Also see the toUnixTimestamp function.

toFixedString(s, N)

Converts a String type argument to a FixedString(N) type (a string with fixed length N). N must be a constant.
If the string has fewer bytes than N, it is padded with null bytes to the right. If the string has more bytes than N, an exception is thrown.

toStringCutToZero(s)

Accepts a String or FixedString argument. Returns the String with the content truncated at the first zero byte found.

Example

Query:

  1. SELECT toFixedString('foo', 8) AS s, toStringCutToZero(s) AS s_cut;

Result:

  1. ┌─s─────────────┬─s_cut─┐
  2. foo\0\0\0\0\0 foo
  3. └───────────────┴───────┘

Query:

  1. SELECT toFixedString('foo\0bar', 8) AS s, toStringCutToZero(s) AS s_cut;

Result:

  1. ┌─s──────────┬─s_cut─┐
  2. foo\0bar\0 foo
  3. └────────────┴───────┘

reinterpretAsUInt(8|16|32|64)

reinterpretAsInt(8|16|32|64)

reinterpretAsFloat(32|64)

reinterpretAsDate

reinterpretAsDateTime

These functions accept a string and interpret the bytes placed at the beginning of the string as a number in host order (little endian). If the string isn’t long enough, the functions work as if the string is padded with the necessary number of null bytes. If the string is longer than needed, the extra bytes are ignored. A date is interpreted as the number of days since the beginning of the Unix Epoch, and a date with time is interpreted as the number of seconds since the beginning of the Unix Epoch.

reinterpretAsString

This function accepts a number or date or date with time, and returns a string containing bytes representing the corresponding value in host order (little endian). Null bytes are dropped from the end. For example, a UInt32 type value of 255 is a string that is one byte long.

reinterpretAsFixedString

This function accepts a number or date or date with time, and returns a FixedString containing bytes representing the corresponding value in host order (little endian). Null bytes are dropped from the end. For example, a UInt32 type value of 255 is a FixedString that is one byte long.

reinterpretAsUUID

This function accepts 16 bytes string, and returns UUID containing bytes representing the corresponding value in network byte order (big-endian). If the string isn’t long enough, the functions work as if the string is padded with the necessary number of null bytes to the end. If the string longer than 16 bytes, the extra bytes at the end are ignored.

Syntax

  1. reinterpretAsUUID(fixed_string)

Parameters

reinterpret(x, T)

Returned value

  • The UUID type value. UUID.

Examples

String to UUID.

Query:

  1. SELECT reinterpret(toInt8(-1), 'UInt8') as int_to_uint,
  2. reinterpret(toInt8(1), 'Float32') as int_to_float,
  3. reinterpret('1', 'UInt32') as string_to_int;

Result:

  1. ┌─reinterpretAsUUID(reverse(unhex('000102030405060708090a0b0c0d0e0f')))─┐
  2. 08090a0b-0c0d-0e0f-0001-020304050607
  3. └───────────────────────────────────────────────────────────────────────┘

Going back and forth from String to UUID.

Query:

  1. WITH
  2. generateUUIDv4() AS uuid,
  3. identity(lower(hex(reverse(reinterpretAsString(uuid))))) AS str,
  4. reinterpretAsUUID(reverse(unhex(str))) AS uuid2
  5. SELECT uuid = uuid2;

Result:

  1. ┌─equals(uuid, uuid2)─┐
  2. 1
  3. └─────────────────────┘

CAST(x, T)

Converts input value x to the T data type.

The syntax CAST(x AS t) is also supported.

Note, that if value x does not fit the bounds of type T, the function overflows. For example, CAST(-1, ‘UInt8’) returns 255.

Example

Query:

  1. SELECT
  2. '2016-06-15 23:00:00' AS timestamp,
  3. CAST(timestamp AS DateTime) AS datetime,
  4. CAST(timestamp AS Date) AS date,
  5. CAST(timestamp, 'String') AS string,
  6. CAST(timestamp, 'FixedString(22)') AS fixed_string;

Result:

  1. ┌─timestamp───────────┬────────────datetime─┬───────date─┬─string──────────────┬─fixed_string──────────────┐
  2. 2016-06-15 23:00:00 2016-06-15 23:00:00 2016-06-15 2016-06-15 23:00:00 2016-06-15 23:00:00\0\0\0
  3. └─────────────────────┴─────────────────────┴────────────┴─────────────────────┴───────────────────────────┘

Conversion to FixedString(N) only works for arguments of type String or FixedString(N).

Type conversion to Nullable and back is supported.

Example

Query:

  1. SELECT toTypeName(x) FROM t_null;

Result:

  1. ┌─toTypeName(x)─┐
  2. Int8
  3. Int8
  4. └───────────────┘

Query:

  1. SELECT toTypeName(CAST(x, 'Nullable(UInt16)')) FROM t_null;

Result:

  1. ┌─toTypeName(CAST(x, 'Nullable(UInt16)'))─┐
  2. Nullable(UInt16)
  3. Nullable(UInt16)
  4. └─────────────────────────────────────────┘

See also

accurateCast(x, T)

Converts x to the T data type.

The difference from cast(x, T) is that accurateCast does not allow overflow of numeric types during cast if type value x does not fit the bounds of type T. For example, accurateCast(-1, 'UInt8') throws an exception.

Example

Query:

  1. SELECT cast(-1, 'UInt8') as uint8;

Result:

  1. ┌─uint8─┐
  2. 255
  3. └───────┘

Query:

  1. SELECT accurateCast(-1, 'UInt8') as uint8;

Result:

  1. Code: 70. DB::Exception: Received from localhost:9000. DB::Exception: Value in column Int8 cannot be safely converted into type UInt8: While processing accurateCast(-1, 'UInt8') AS uint8.

accurateCastOrNull(x, T)

Converts input value x to the specified data type T. Always returns Nullable type and returns NULL if the casted value is not representable in the target type.

Syntax

  1. accurateCastOrNull(x, T)

Parameters

  • x — Input value.
  • T — The name of the returned data type.

Returned value

  • The value, converted to the specified data type T.

Example

Query:

  1. SELECT toTypeName(accurateCastOrNull(5, 'UInt8'));

Result:

  1. ┌─toTypeName(accurateCastOrNull(5, 'UInt8'))─┐
  2. Nullable(UInt8)
  3. └────────────────────────────────────────────┘

Query:

  1. SELECT
  2. accurateCastOrNull(-1, 'UInt8') as uint8,
  3. accurateCastOrNull(128, 'Int8') as int8,
  4. accurateCastOrNull('Test', 'FixedString(2)') as fixed_string;

Result:

  1. ┌─uint8─┬─int8─┬─fixed_string─┐
  2. ᴺᵁᴸᴸ ᴺᵁᴸᴸ ᴺᵁᴸᴸ
  3. └───────┴──────┴──────────────┘

toInterval(Year|Quarter|Month|Week|Day|Hour|Minute|Second)

Converts a Number type argument to an Interval data type.

Syntax

  1. toIntervalSecond(number)
  2. toIntervalMinute(number)
  3. toIntervalHour(number)
  4. toIntervalDay(number)
  5. toIntervalWeek(number)
  6. toIntervalMonth(number)
  7. toIntervalQuarter(number)
  8. toIntervalYear(number)

Arguments

  • number — Duration of interval. Positive integer number.

Returned values

  • The value in Interval data type.

Example

Query:

  1. WITH
  2. toDate('2019-01-01') AS date,
  3. INTERVAL 1 WEEK AS interval_week,
  4. toIntervalWeek(1) AS interval_to_week
  5. SELECT
  6. date + interval_week,
  7. date + interval_to_week;

Result:

  1. ┌─plus(date, interval_week)─┬─plus(date, interval_to_week)─┐
  2. 2019-01-08 2019-01-08
  3. └───────────────────────────┴──────────────────────────────┘

parseDateTimeBestEffort

Converts a date and time in the String representation to DateTime data type.

The function parses ISO 8601, RFC 1123 - 5.2.14 RFC-822 Date and Time Specification, ClickHouse’s and some other date and time formats.

Syntax

  1. parseDateTimeBestEffort(time_string [, time_zone])

Arguments

  • time_string — String containing a date and time to convert. String.
  • time_zone — Time zone. The function parses time_string according to the time zone. String.

Supported non-standard formats

  • A string containing 9..10 digit unix timestamp.
  • A string with a date and a time component: YYYYMMDDhhmmss, DD/MM/YYYY hh:mm:ss, DD-MM-YY hh:mm, YYYY-MM-DD hh:mm:ss, etc.
  • A string with a date, but no time component: YYYY, YYYYMM, YYYY*MM, DD/MM/YYYY, DD-MM-YY etc.
  • A string with a day and time: DD, DD hh, DD hh:mm. In this case YYYY-MM are substituted as 2000-01.
  • A string that includes the date and time along with time zone offset information: YYYY-MM-DD hh:mm:ss ±h:mm, etc. For example, 2020-12-12 17:36:00 -5:00.

For all of the formats with separator the function parses months names expressed by their full name or by the first three letters of a month name. Examples: 24/DEC/18, 24-Dec-18, 01-September-2018.

Returned value

  • time_string converted to the DateTime data type.

Examples

Query:

  1. SELECT parseDateTimeBestEffort('12/12/2020 12:12:57')
  2. AS parseDateTimeBestEffort;

Result:

  1. ┌─parseDateTimeBestEffort─┐
  2. 2020-12-12 12:12:57
  3. └─────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffort('Sat, 18 Aug 2018 07:22:16 GMT', 'Europe/Moscow')
  2. AS parseDateTimeBestEffort;

Result:

  1. ┌─parseDateTimeBestEffort─┐
  2. 2018-08-18 10:22:16
  3. └─────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffort('1284101485')
  2. AS parseDateTimeBestEffort;

Result:

  1. ┌─parseDateTimeBestEffort─┐
  2. 2015-07-07 12:04:41
  3. └─────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffort('2018-12-12 10:12:12')
  2. AS parseDateTimeBestEffort;

Result:

  1. ┌─parseDateTimeBestEffort─┐
  2. 2018-12-12 10:12:12
  3. └─────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffort('10 20:19');

Result:

  1. ┌─parseDateTimeBestEffort('10 20:19')─┐
  2. 2000-01-10 20:19:00
  3. └─────────────────────────────────────┘

See Also

parseDateTimeBestEffortUS

This function is similar to parseDateTimeBestEffort, the only difference is that this function prefers US date format (MM/DD/YYYY etc.) in case of ambiguity.

Syntax

  1. parseDateTimeBestEffortUS(time_string [, time_zone])

Arguments

  • time_string — String containing a date and time to convert. String.
  • time_zone — Time zone. The function parses time_string according to the time zone. String.

Supported non-standard formats

  • A string containing 9..10 digit unix timestamp.
  • A string with a date and a time component: YYYYMMDDhhmmss, MM/DD/YYYY hh:mm:ss, MM-DD-YY hh:mm, YYYY-MM-DD hh:mm:ss, etc.
  • A string with a date, but no time component: YYYY, YYYYMM, YYYY*MM, MM/DD/YYYY, MM-DD-YY etc.
  • A string with a day and time: DD, DD hh, DD hh:mm. In this case, YYYY-MM are substituted as 2000-01.
  • A string that includes the date and time along with time zone offset information: YYYY-MM-DD hh:mm:ss ±h:mm, etc. For example, 2020-12-12 17:36:00 -5:00.

Returned value

  • time_string converted to the DateTime data type.

Examples

Query:

  1. SELECT parseDateTimeBestEffortUS('09/12/2020 12:12:57')
  2. AS parseDateTimeBestEffortUS;

Result:

  1. ┌─parseDateTimeBestEffortUS─┐
  2. 2020-09-12 12:12:57
  3. └─────────────────────────——┘

Query:

  1. SELECT parseDateTimeBestEffortUS('09-12-2020 12:12:57')
  2. AS parseDateTimeBestEffortUS;

Result:

  1. ┌─parseDateTimeBestEffortUS─┐
  2. 2020-09-12 12:12:57
  3. └─────────────────────────——┘

Query:

  1. SELECT parseDateTimeBestEffortUS('09.12.2020 12:12:57')
  2. AS parseDateTimeBestEffortUS;

Result:

  1. ┌─parseDateTimeBestEffortUS─┐
  2. 2020-09-12 12:12:57
  3. └─────────────────────────——┘

parseDateTimeBestEffortOrNull

Same as for parseDateTimeBestEffort except that it returns null when it encounters a date format that cannot be processed.

parseDateTimeBestEffortOrZero

Same as for parseDateTimeBestEffort except that it returns zero date or zero date time when it encounters a date format that cannot be processed.

parseDateTimeBestEffortUSOrNull

Same as parseDateTimeBestEffortUS function except that it returns NULL when it encounters a date format that cannot be processed.

Syntax

  1. parseDateTimeBestEffortUSOrNull(time_string[, time_zone])

Parameters

  • time_string — String containing a date or date with time to convert. The date must be in the US date format (MM/DD/YYYY, etc). String.
  • time_zoneTimezone. The function parses time_string according to the timezone. Optional. String.

Supported non-standard formats

  • A string containing 9..10 digit unix timestamp.
  • A string with a date and a time components: YYYYMMDDhhmmss, MM/DD/YYYY hh:mm:ss, MM-DD-YY hh:mm, YYYY-MM-DD hh:mm:ss, etc.
  • A string with a date, but no time component: YYYY, YYYYMM, YYYY*MM, MM/DD/YYYY, MM-DD-YY, etc.
  • A string with a day and time: DD, DD hh, DD hh:mm. In this case, YYYY-MM are substituted with 2000-01.
  • A string that includes date and time along with timezone offset information: YYYY-MM-DD hh:mm:ss ±h:mm, etc. For example, 2020-12-12 17:36:00 -5:00.

Returned values

  • time_string converted to the DateTime data type.
  • NULL if the input string cannot be converted to the DateTime data type.

Examples

Query:

  1. SELECT parseDateTimeBestEffortUSOrNull('02/10/2021 21:12:57') AS parseDateTimeBestEffortUSOrNull;

Result:

  1. ┌─parseDateTimeBestEffortUSOrNull─┐
  2. 2021-02-10 21:12:57
  3. └─────────────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffortUSOrNull('02-10-2021 21:12:57 GMT', 'Europe/Moscow') AS parseDateTimeBestEffortUSOrNull;

Result:

  1. ┌─parseDateTimeBestEffortUSOrNull─┐
  2. 2021-02-11 00:12:57
  3. └─────────────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffortUSOrNull('02.10.2021') AS parseDateTimeBestEffortUSOrNull;

Result:

  1. ┌─parseDateTimeBestEffortUSOrNull─┐
  2. 2021-02-10 00:00:00
  3. └─────────────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffortUSOrNull('10.2021') AS parseDateTimeBestEffortUSOrNull;

Result:

  1. ┌─parseDateTimeBestEffortUSOrNull─┐
  2. ᴺᵁᴸᴸ
  3. └─────────────────────────────────┘

parseDateTimeBestEffortUSOrZero

Same as parseDateTimeBestEffortUS function except that it returns zero date (1970-01-01) or zero date with time (1970-01-01 00:00:00) when it encounters a date format that cannot be processed.

Syntax

  1. parseDateTimeBestEffortUSOrZero(time_string[, time_zone])

Parameters

  • time_string — String containing a date or date with time to convert. The date must be in the US date format (MM/DD/YYYY, etc). String.
  • time_zoneTimezone. The function parses time_string according to the timezone. Optional. String.

Supported non-standard formats

  • A string containing 9..10 digit unix timestamp.
  • A string with a date and a time components: YYYYMMDDhhmmss, MM/DD/YYYY hh:mm:ss, MM-DD-YY hh:mm, YYYY-MM-DD hh:mm:ss, etc.
  • A string with a date, but no time component: YYYY, YYYYMM, YYYY*MM, MM/DD/YYYY, MM-DD-YY, etc.
  • A string with a day and time: DD, DD hh, DD hh:mm. In this case, YYYY-MM are substituted with 2000-01.
  • A string that includes date and time along with timezone offset information: YYYY-MM-DD hh:mm:ss ±h:mm, etc. For example, 2020-12-12 17:36:00 -5:00.

Returned values

  • time_string converted to the DateTime data type.
  • Zero date or zero date with time if the input string cannot be converted to the DateTime data type.

Examples

Query:

  1. SELECT parseDateTimeBestEffortUSOrZero('02/10/2021 21:12:57') AS parseDateTimeBestEffortUSOrZero;

Result:

  1. ┌─parseDateTimeBestEffortUSOrZero─┐
  2. 2021-02-10 21:12:57
  3. └─────────────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffortUSOrZero('02-10-2021 21:12:57 GMT', 'Europe/Moscow') AS parseDateTimeBestEffortUSOrZero;

Result:

  1. ┌─parseDateTimeBestEffortUSOrZero─┐
  2. 2021-02-11 00:12:57
  3. └─────────────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffortUSOrZero('02.10.2021') AS parseDateTimeBestEffortUSOrZero;

Result:

  1. ┌─parseDateTimeBestEffortUSOrZero─┐
  2. 2021-02-10 00:00:00
  3. └─────────────────────────────────┘

Query:

  1. SELECT parseDateTimeBestEffortUSOrZero('02.2021') AS parseDateTimeBestEffortUSOrZero;

Result:

  1. ┌─parseDateTimeBestEffortUSOrZero─┐
  2. 1970-01-01 00:00:00
  3. └─────────────────────────────────┘

toLowCardinality

Converts input parameter to the LowCardianlity version of same data type.

To convert data from the LowCardinality data type use the CAST function. For example, CAST(x as String).

Syntax

  1. toLowCardinality(expr)

Arguments

Returned values

  • Result of expr.

Type: LowCardinality(expr_result_type)

Example

Query:

  1. SELECT toLowCardinality('1');

Result:

  1. ┌─toLowCardinality('1')─┐
  2. 1
  3. └───────────────────────┘

toUnixTimestamp64Milli

toUnixTimestamp64Micro

toUnixTimestamp64Nano

Converts a DateTime64 to a Int64 value with fixed sub-second precision. Input value is scaled up or down appropriately depending on it precision. Please note that output value is a timestamp in UTC, not in timezone of DateTime64.

Syntax

  1. toUnixTimestamp64Milli(value)

Arguments

  • value — DateTime64 value with any precision.

Returned value

  • value converted to the Int64 data type.

Examples

Query:

  1. WITH toDateTime64('2019-09-16 19:20:12.345678910', 6) AS dt64
  2. SELECT toUnixTimestamp64Milli(dt64);

Result:

  1. ┌─toUnixTimestamp64Milli(dt64)─┐
  2. 1568650812345
  3. └──────────────────────────────┘
  1. WITH toDateTime64('2019-09-16 19:20:12.345678910', 6) AS dt64
  2. SELECT toUnixTimestamp64Nano(dt64);

Result:

  1. ┌─toUnixTimestamp64Nano(dt64)─┐
  2. 1568650812345678000
  3. └─────────────────────────────┘

fromUnixTimestamp64Milli

fromUnixTimestamp64Micro

fromUnixTimestamp64Nano

Converts an Int64 to a DateTime64 value with fixed sub-second precision and optional timezone. Input value is scaled up or down appropriately depending on it’s precision. Please note that input value is treated as UTC timestamp, not timestamp at given (or implicit) timezone.

Syntax

  1. fromUnixTimestamp64Milli(value [, ti])

Arguments

  • valueInt64 value with any precision.
  • timezoneString (optional) timezone name of the result.

Returned value

  • value converted to the DateTime64 data type.

Example

Query:

  1. WITH CAST(1234567891011, 'Int64') AS i64
  2. SELECT fromUnixTimestamp64Milli(i64, 'UTC');

Result:

  1. ┌─fromUnixTimestamp64Milli(i64, 'UTC')─┐
  2. 2009-02-13 23:31:31.011
  3. └──────────────────────────────────────┘

formatRow

Converts arbitrary expressions into a string via given format.

Syntax

  1. formatRow(format, x, y, ...)

Arguments

  • format — Text format. For example, CSV, TSV.
  • x,y, … — Expressions.

Returned value

  • A formatted string (for text formats it’s usually terminated with the new line character).

Example

Query:

  1. SELECT formatRow('CSV', number, 'good')
  2. FROM numbers(3);

Result:

  1. ┌─formatRow('CSV', number, 'good')─┐
  2. 0,"good"
  3. 1,"good"
  4. 2,"good"
  5. └──────────────────────────────────┘

formatRowNoNewline

Converts arbitrary expressions into a string via given format. The function trims the last \n if any.

Syntax

  1. formatRowNoNewline(format, x, y, ...)

Arguments

  • format — Text format. For example, CSV, TSV.
  • x,y, … — Expressions.

Returned value

  • A formatted string.

Example

Query:

  1. SELECT formatRowNoNewline('CSV', number, 'good')
  2. FROM numbers(3);

Result:

  1. ┌─formatRowNoNewline('CSV', number, 'good')─┐
  2. 0,"good"
  3. 1,"good"
  4. 2,"good"
  5. └───────────────────────────────────────────┘

Original article