Clickhouse 类型转换函数实战总结

文章目录

数据精度丢失问题

当把一个值从一个类型转换为另外一个类型的时候,你需要注意的是这是一个不安全的操作,可能导致数据精度的丢失。数据精度丢失一般发生在你将一个大的数据类型转换为小的数据类型的时候,或者你把两个不同的数据类型相互转换的时候。
ClickHouse和C++有相同的类型转换行为。

toInt(8|16|32|64) 转换一个输入值为Int类型

函数说明:
toInt8(expr) — 结果为Int8数据类型。
toInt16(expr) — 结果为Int16数据类型。
toInt32(expr) — 结果为Int32数据类型。
toInt64(expr) — 结果为Int64数据类型。
  • 参数:
    expr — 表达式返回一个数字或者代表数值类型的字符串。不支持二进制、八进制、十六进制的数字形式,有效数字之前的0也会被忽略。 返回值
  • 返回值:
    1、整形在Int8,Int16, Int32,或者 Int64 的数据类型。
    2、函数使用rounding towards zero原则,这意味着会截断丢弃小数部分的数值。
    3、NaNInf转换是不确定的。具体使用的时候,请参考数值类型转换常见的问题。
函数实例:
localhost :) SELECT toInt64(nan), toInt32(32), toInt16('16'), toInt8(8.8)

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

Query id: 8ecc59d4-598a-44c5-86ac-bb495c73a17f

┌─────────toInt64(nan)─┬─toInt32(32)─┬─toInt16('16')─┬─toInt8(8.8)─┐
│ -922337203685477580832168 │
└──────────────────────┴─────────────┴───────────────┴─────────────┘

1 rows in set. Elapsed: 0.002 sec.

toInt(8|16|32|64)OrZero

函数说明:
toInt8OrZero(ex pr) — 结果为Int8数据类型,失败返回0。
toInt16OrZero(expr) — 结果为Int16数据类型,失败返回0。
toInt32OrZero(expr) — 结果为Int32数据类型,失败返回0。
toInt64OrZero(expr) — 结果为Int64数据类型,失败返回0
  • 参数:
    expr — 表达式返回一个字符类型。
  • 返回值:
    整形在Int8,Int16, Int32,或者 Int64 的数据类型,如果转换失败直接返回0。
函数实例:
localhost :) select toInt64OrZero('123123'), toInt8OrZero('123qwe123')

SELECT
    toInt64OrZero('123123'),
    toInt8OrZero('123qwe123')

Query id: 26572397-e5d9-41e4-b46a-7a0bdf90b01e

┌─toInt64OrZero('123123')─┬─toInt8OrZero('123qwe123')─┐
│                  1231230 │
└─────────────────────────┴───────────────────────────┘

1 rows in set. Elapsed: 0.009 sec.

toInt(8|16|32|64)OrNull

函数说明:
toInt8OrNull (expr) — 结果为Int8数据类型,失败返回Null。
toInt16OrNull (expr) — 结果为Int16数据类型,失败返回Null。
toInt32OrNull (expr) — 结果为Int32数据类型,失败返回Null。
toInt64OrNull (expr) — 结果为Int64数据类型,失败返回Null

这个函数需要一个字符类型的入参,然后尝试把它转为Int (8 | 16 | 32 | 64),如果转换失败直接返回NULL

函数实例:
localhost :) select toInt64OrNull('123123'), toInt8OrNull('123qwe123')

SELECT
    toInt64OrNull('123123'),
    toInt8OrNull('123qwe123')

Query id: de0dc0f3-2881-47d6-ba46-9bfad78d25e7

┌─toInt64OrNull('123123')─┬─toInt8OrNull('123qwe123')─┐
│                  123123 │                      ᴺᵁᴸᴸ │
└─────────────────────────┴───────────────────────────┘

1 rows in set. Elapsed: 0.026 sec.

toUInt(8|16|32|64) 转换一个输入值到UInt类型。

函数说明:

这个函数包括:

toUInt8(expr) — 结果为UInt8数据类型。
toUInt16(expr) — 结果为UInt16数据类型。
toUInt32(expr) — 结果为UInt32数据类型。
toUInt64(expr) — 结果为UInt64数据类型。
  • 参数:
    expr — 表达式返回一个数字或者代表数值类型的字符串。不支持二进制、八进制、十六进制的数字形式,有效数字之前的0也会被忽略。
  • 返回值:
    1、整形在UInt8, UInt16, UInt32,或者 UInt64 的数据类型。 函数使用 rounding towards zero原则,这意味着会截断丢弃小数部分的数值。
    2、 对于负数、NaN 和 Inf来说转换的结果是不确定的。如果你传入一个负数,比如:'-32'ClickHouse会抛出异常。具体使用的时候,请参考数值类型转换常见的问题。
函数实例:
localhost :) SELECT toUInt64(nan), toUInt32(-32), toUInt16('16'), toUInt8(8.8),toUInt64(inf),toUInt64(-inf)

SELECT
    toUInt64(nan),
    toUInt32(-32),
    toUInt16('16'),
    toUInt8(8.8),
    toUInt64(inf),
    toUInt64(-inf)

Query id: 254cb8a2-c6d9-4b14-b7ae-c9e1c9293ab7

┌─toUInt64(nan)─┬─toUInt32(-32)─┬─toUInt16('16')─┬─toUInt8(8.8)─┬─toUInt64(inf)─┬──────toUInt64(-inf)─┐
│             0429496726416809223372036854775808 │
└───────────────┴───────────────┴────────────────┴──────────────┴───────────────┴─────────────────────┘

1 rows in set. Elapsed: 0.024 sec.

toUInt(8|16|32|64)OrZero

函数说明:
toUInt8OrZero(expr) — 结果为Int8数据类型,失败返回0。
toUInt16OrZero(expr) — 结果为Int16数据类型,失败返回0。
toUInt32OrZero(expr) — 结果为Int32数据类型,失败返回0。
toUInt64OrZero(expr) — 结果为Int64数据类型,失败返回0
  • 参数:
    expr — 表达式返回一个字符类型。
  • 返回值:
    整形在Int8,Int16, Int32,或者 Int64 的数据类型,如果转换失败直接返回0。
函数实例:
localhost :) SELECT toUInt8OrZero('32'), toUInt8OrZero('16'), toUInt8OrZero('8wer8'),toUInt8OrZero('8.8')

SELECT
    toUInt8OrZero('32'),
    toUInt8OrZero('16'),
    toUInt8OrZero('8wer8'),
    toUInt8OrZero('8.8')

Query id: 415a109f-a6e3-422a-964e-44200b10b34d

┌─toUInt8OrZero('32')─┬─toUInt8OrZero('16')─┬─toUInt8OrZero('8wer8')─┬─toUInt8OrZero('8.8')─┐
│                  321600 │
└─────────────────────┴─────────────────────┴────────────────────────┴──────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

toUInt(8|16|32|64)OrNull

函数说明:
toUInt8OrNull(expr) — 结果为Int8数据类型,失败返回Null。
toUInt16OrNull(expr) — 结果为Int16数据类型,失败返回Null。
toUInt32OrNull(expr) — 结果为Int32数据类型,失败返回Null。
toUInt64OrNull(expr) — 结果为Int64数据类型,失败返回Null
  • 参数:
    expr — 表达式返回一个字符类型。
  • 返回值:
    整形在Int8,Int16, Int32,或者 Int64 的数据类型,如果转换失败直接返回Null。
函数实例:
localhost :) SELECT toUInt8OrNull('32'), toUInt8OrNull('16'), toUInt8OrNull('8wer8'),toUInt8OrNull('8.8')

SELECT
    toUInt8OrNull('32'),
    toUInt8OrNull('16'),
    toUInt8OrNull('8wer8'),
    toUInt8OrNull('8.8')

Query id: 65ecee43-aa01-48ec-94ea-76406b347dff

┌─toUInt8OrNull('32')─┬─toUInt8OrNull('16')─┬─toUInt8OrNull('8wer8')─┬─toUInt8OrNull('8.8')─┐
│                  3216 │                   ᴺᵁᴸᴸ │                 ᴺᵁᴸᴸ │
└─────────────────────┴─────────────────────┴────────────────────────┴──────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toFloat(32|64)

函数说明:
toFloat32(expr) — 结果为Float32数据类型。
toFloat64(expr) — 结果为Float64数据类型。
  • 参数:
    expr — 表达式返回一个数字或者代表数值类型的字符串。不支持二进制、八进制、十六进制的数字形式,有效数字之前的0也会被忽略。 返回值
  • 返回值:
    1、在Float32或者Float64的数据类型。
    2、函数使用rounding towards zero原则,这意味着会截断丢弃小数部分的数值。
    3、NaNInf转换是不确定的。具体使用的时候,请参考数值类型转换常见的问题。
    4、字符串转换正常都会报错,但是在使用类似'2e3'这种clickhoues会将e3识别为科学技术法1000
函数实例:
localhost :) select toFloat32(1),toFloat32('3.1111111111'),toFloat32('2e3'),toFloat32(nan),toFloat32(inf),toFloat32(-inf),toFloat32(3.11)

SELECT
    toFloat32(1),
    toFloat32('3.1111111111'),
    toFloat32('2e3'),
    toFloat32(nan),
    toFloat32(inf),
    toFloat32(-inf),
    toFloat32(3.11)

Query id: e863f010-2299-4737-b60e-10551fe84461

┌─toFloat32(1)─┬─toFloat32('3.1111111111')─┬─toFloat32('2e3')─┬─toFloat32(nan)─┬─toFloat32(inf)─┬─toFloat32(-inf)─┬─toFloat32(3.11)─┐
│            13.11111122000 │            nan │            inf │            -inf │            3.11 │
└──────────────┴───────────────────────────┴──────────────────┴────────────────┴────────────────┴─────────────────┴─────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toFloat(32|64)OrZero

函数说明:
toFloat32OrZero(expr) — 结果为Float32数据类型,失败返回0。
toFloat64OrZero(expr) — 结果为Float64数据类型,失败返回0
  • 参数:
    expr — 表达式返回一个字符类型。
  • 返回值:
    Float32或者Float64的数据类型,失败返回0。
函数实例:
localhost :) select toFloat32OrZero('qq'),toFloat32OrZero('3.00'),toFloat32OrZero('123.1233333')

SELECT
    toFloat32OrZero('qq'),
    toFloat32OrZero('3.00'),
    toFloat32OrZero('123.1233333')

Query id: 6ffc2dfc-0b52-4e31-9f35-70c19059d911

┌─toFloat32OrZero('qq')─┬─toFloat32OrZero('3.00')─┬─toFloat32OrZero('123.1233333')─┐
│                     03123.12334 │
└───────────────────────┴─────────────────────────┴────────────────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

toFloat(32|64)OrNull

函数说明:
toFloat32OrNull(expr) — 结果为Float32数据类型,失败返回Null。
toFloat64OrNull(expr) — 结果为Float64数据类型,失败返回Null
  • 参数:
    expr — 表达式返回一个字符类型。
  • 返回值:
    Float32或者Float64的数据类型,失败反馈null。
函数实例:
localhost :) select toFloat32OrNull('qq'),toFloat32OrNull('3.00'),toFloat32OrNull('123.1233333')

SELECT
    toFloat32OrNull('qq'),
    toFloat32OrNull('3.00'),
    toFloat32OrNull('123.1233333')

Query id: f55c3833-4d89-4588-a6fd-ba3ceecbb739

┌─toFloat32OrNull('qq')─┬─toFloat32OrNull('3.00')─┬─toFloat32OrNull('123.1233333')─┐
│                  ᴺᵁᴸᴸ │                       3123.12334 │
└───────────────────────┴─────────────────────────┴────────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toDate

函数说明:
toDate(exp) — 结果为Date数据类型。
  • 参数:
    1、exp - 表达式返回一个数字、字符串、日期、时间戳。
  • 返回值:
    1、Date数据类型。
    2、数字会在1970-01-1的基础上加相应的数字天。
函数实例:
localhost :) select toDate(1),toDate('1970-01-02'),toDate(today()),toDate(now())

SELECT
    toDate(1),
    toDate('1970-01-02'),
    toDate(today()),
    toDate(now())

Query id: e20fff27-e981-42b3-a4e1-90d5d80c9936

┌──toDate(1)─┬─toDate('1970-01-02')─┬─toDate(today())─┬─toDate(now())─┐
│ 1970-01-021970-01-022021-05-212021-05-21 │
└────────────┴──────────────────────┴─────────────────┴───────────────┘

1 rows in set. Elapsed: 0.003 sec.

toDateOrZero

函数说明:
toDateOrZero(exp) — 结果为Date数据类型,失败返回1970-01-01
  • 参数:
    1、exp - 表达式返回日期格式字符串、时间格式字符串、数字或者字符字符串。
  • 返回值:
    1、Date数据类型。失败返回1970-01-01
    2、日期格式字符串、时间格式字符串,返回该时间日期。
    3、数字或者字符字符串,会转换失败返回1970-01-01
函数实例:
localhost :) select toDateOrZero('2021-05-21 10:20:03'),toDateOrZero('2021-05-21'),toDateOrZero('21'),toDateOrZero('q@')

SELECT
    toDateOrZero('2021-05-21 10:20:03'),
    toDateOrZero('2021-05-21'),
    toDateOrZero('21'),
    toDateOrZero('q')

Query id: 8838a8f9-e741-46b9-8e33-62b46ed1d60c

┌─toDateOrZero('2021-05-21 10:20:03')─┬─toDateOrZero('2021-05-21')─┬─toDateOrZero('21')─┬─toDateOrZero('q@')─┐
│                          2021-05-212021-05-211970-01-011970-01-01 │
└─────────────────────────────────────┴────────────────────────────┴────────────────────┴───────────────────┘

1 rows in set. Elapsed: 0.002 sec.

toDateOrNull

函数说明:
toDateOrNull(exp) — 结果为Date数据类型,失败返回Null
  • 参数:
    1、exp - 表达式返回日期格式字符串、时间格式字符串、数字或者字符字符串。
  • 返回值:
    1、Date数据类型。失败返回Null
    2、日期格式字符串、时间格式字符串,返回该时间日期。
    3、数字或者字符字符串,会转换失败返回Null
函数实例:
localhost :) select toDateOrNull('2021-05-21 10:20:03'),toDateOrNull('2021-05-21'),toDateOrNull('21'),toDateOrNull('q@')

SELECT
    toDateOrNull('2021-05-21 10:20:03'),
    toDateOrNull('2021-05-21'),
    toDateOrNull('21'),
    toDateOrNull('q@')

Query id: 2d993458-43d4-4125-b228-791d65c379aa

┌─toDateOrNull('2021-05-21 10:20:03')─┬─toDateOrNull('2021-05-21')─┬─toDateOrNull('21')─┬─toDateOrNull('q@')─┐
│                          2021-05-212021-05-21 │               ᴺᵁᴸᴸ │               ᴺᵁᴸᴸ │
└─────────────────────────────────────┴────────────────────────────┴────────────────────┴────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

toDateTime

函数说明:
toDateTime(exp) — 结果为DateTime数据类型。
  • 参数:
    1、exp - 表达式返回时间格式字符串、数字、Date类型数据、DateTime类型数据。
  • 返回值:
    1、DateTime数据类型。
    2、时间格式字符串,返回该时间的DateTime类型。
    3、数字会直接在1970-01-01 08:00:00的数字秒数。
函数实例:
localhost :) select toDateTime(today()),toDateTime(now()),toDateTime(1),toDateTime('2021-05-21 11:37:35')

SELECT
    toDateTime(today()),
    toDateTime(now()),
    toDateTime(1),
    toDateTime('2021-05-21 11:37:35')

Query id: 72da2705-d9ac-4d0c-987d-6ae7ff5308ef

┌─toDateTime(today())─┬───toDateTime(now())─┬───────toDateTime(1)─┬─toDateTime('2021-05-21 11:37:35')─┐
│ 2021-05-21 00:00:002021-05-21 11:43:151970-01-01 08:00:012021-05-21 11:37:35 │
└─────────────────────┴─────────────────────┴─────────────────────┴───────────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toDateTimeOrZero

函数说明:
toDateTimeOrZero(exp) — 结果为DateTime数据类型,失败返回1970-01-01 08:00:00
  • 参数:
    1、exp - 表达式返回时间格式字符串、字符串。
  • 返回值:
    1、DateTime数据类型,失败返回1970-01-01 08:00:00
    2、时间格式字符串,返回该时间的DateTime类型。
    3、字符串,会转换失败返回1970-01-01 08:00:00
函数实例:
localhost :) select toDateTimeOrZero('1'),toDateTimeOrZero('2021-05-21 11:37:35'),toDateTimeOrZero('1www')

SELECT
    toDateTimeOrZero('1'),
    toDateTimeOrZero('2021-05-21 11:37:35'),
    toDateTimeOrZero('1www')

Query id: dc19e620-0845-4a2f-b276-c57cf3967f92

┌─toDateTimeOrZero('1')─┬─toDateTimeOrZero('2021-05-21 11:37:35')─┬─toDateTimeOrZero('1www')─┐
│   1970-01-01 08:00:002021-05-21 11:37:351970-01-01 08:00:00 │
└───────────────────────┴─────────────────────────────────────────┴──────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toDateTimeOrNull

函数说明:
toDateTimeOrNull(exp) — 结果为DateTime数据类型,失败返回Null
  • 参数:
    1、exp - 表达式返回时间格式字符串、字符串。
  • 返回值:
    1、DateTime数据类型,失败返回Null
    2、时间格式字符串,返回该时间的DateTime类型。
    3、字符串,会转换失败返回Null
函数实例:
localhost :) select toDateTimeOrNull('1'),toDateTimeOrNull('2021-05-21 11:37:35'),toDateTimeOrNull('1www')

SELECT
    toDateTimeOrNull('1'),
    toDateTimeOrNull('2021-05-21 11:37:35'),
    toDateTimeOrNull('1www')

Query id: 3aa58644-5f1c-4d3f-b20a-083678274604

┌─toDateTimeOrNull('1')─┬─toDateTimeOrNull('2021-05-21 11:37:35')─┬─toDateTimeOrNull('1www')─┐
│                  ᴺᵁᴸᴸ │                     2021-05-21 11:37:35 │                     ᴺᵁᴸᴸ │
└───────────────────────┴─────────────────────────────────────────┴──────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toDecimal(32|64|128)

函数说明:

转换 valueDecimal类型的值,其中精度为S

  • 参数:
    1、value可以是一个数字或者一个字符串。
    2、S 指定小数位的精度。
toDecimal32(value, S) --S从[0,9]-对于Decimal32(S),S最多可以指定9位,结果为Decimal32(S)类型
toDecimal64(value, S) --S从[0,18]-对于Decimal64(S),S最多可以指定18位,结果为Decimal64(S)类型
toDecimal128(value, S) --S从[0,38]-对于Decimal128(S),S最多可以指定38位,结果为Decimal128(S)类型
函数实例:
localhost :) select toDecimal32(0.3,9),toDecimal64(0.3,18),toDecimal128(0.339394321,38),toDecimal128(0.339394321,0),toDecimal32(0.3393943212131242112412,9),toDecimal32('0.3',9)

SELECT
    toDecimal32(0.3, 9),
    toDecimal64(0.3, 18),
    toDecimal128(0.339394321, 38),
    toDecimal128(0.339394321, 0),
    toDecimal32(0.3393943212131242, 9),
    toDecimal32('0.3', 9)

Query id: 6c46cfcd-fc39-4540-9bf6-9f72462f3361

┌─toDecimal32(0.3, 9)─┬─toDecimal64(0.3, 18)─┬────────────toDecimal128(0.339394321, 38)─┬─toDecimal128(0.339394321, 0)─┬─toDecimal32(0.3393943212131242, 9)─┬─toDecimal32('0.3', 9)─┐
│         0.3000000000.3000000000000000000.3393943210000000024696051305366631219200.3393943210.300000000 │
└─────────────────────┴──────────────────────┴──────────────────────────────────────────┴──────────────────────────────┴────────────────────────────────────┴───────────────────────┘

1 rows in set. Elapsed: 0.004 sec.

toDecimal(32|64|128)OrNull

函数说明:

转换一个输入的字符到Nullable(Decimal(P,S))类型的数据。这个函数包括:

toDecimal32OrNull(expr, S) --结果为Nullable(Decimal32(S))数据类型。
toDecimal64OrNull(expr, S) --结果为Nullable(Decimal64(S))数据类型。
toDecimal128OrNull(expr, S) --结果为Nullable(Decimal128(S))数据类型。
  • 使用场景:
    如果在解析输入值发生错误的时候你希望得到一个NULL值而不是抛出异常,你可以使用该函数。
  • 参数:
    1、expr — 表达式返回一个String类型的数据。ClickHouse倾向于文本类型的表示带小数类型的数值,比如'1.111'
    2、S — 小数位的精度。
  • 返回值:
    Nullable(Decimal(P,S))类型的数据,包括:
    1、如果有的话,小数位S
    2、如果解析错误或者输入的数字的小数位多于S,那结果为NULL
函数实例:
localhost :) SELECT toDecimal32OrNull('0.3',9),toDecimal64OrNull('0.3',18),toDecimal128OrNull('0.3',38),toDecimal128OrNull('Q',38)

SELECT
    toDecimal32OrNull('0.3', 9),
    toDecimal64OrNull('0.3', 18),
    toDecimal128OrNull('0.3', 38),
    toDecimal128OrNull('Q', 38)

Query id: 77a5f28d-fa0a-4e84-a8e9-ae080fe098cf

┌─toDecimal32OrNull('0.3', 9)─┬─toDecimal64OrNull('0.3', 18)─┬────────────toDecimal128OrNull('0.3', 38)─┬─toDecimal128OrNull('Q', 38)─┐
│                 0.3000000000.3000000000000000000.30000000000000000000000000000000000000 │                        ᴺᵁᴸᴸ │
└─────────────────────────────┴──────────────────────────────┴──────────────────────────────────────────┴─────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toDecimal(32|64|128)OrZero

转换输入值为Decimal(P,S)类型数据。这个函数包括:

toDecimal32OrZero( expr, S) -- 结果为Decimal32(S) 数据类型。
toDecimal64OrZero( expr, S) -- 结果为Decimal64(S) 数据类型。
toDecimal128OrZero( expr, S) -- 结果为Decimal128(S) 数据类型。
  • 使用场景:
    当解析错误的时候,你不需要抛出异常而希望得到0值,你可以使用该函数。
  • 参数:
    1、expr — 表达式返回一个String类型的数据。 ClickHouse倾向于文本类型的表示带小数类型的数值,比如'1.111'
    2、S — 小数位的精度。
  • 返回值:
    Nullable(Decimal(P,S))类型的数据,包括:
    1、如果有的话,小数位S
    2、如果解析错误或者输入的数字的小数位多于S,那结果为小数位精度为S0
函数实例:
localhost :) SELECT toDecimal32OrZero('0.3',9),toDecimal64OrZero('0.3',18),toDecimal128OrZero('0.3',38),toDecimal128OrZero('Q',38)

SELECT
    toDecimal32OrZero('0.3', 9),
    toDecimal64OrZero('0.3', 18),
    toDecimal128OrZero('0.3', 38),
    toDecimal128OrZero('Q', 38)

Query id: d821e47d-f531-4185-863a-c069d20e5f62

┌─toDecimal32OrZero('0.3', 9)─┬─toDecimal64OrZero('0.3', 18)─┬────────────toDecimal128OrZero('0.3', 38)─┬──────────────toDecimal128OrZero('Q', 38)─┐
│                 0.3000000000.3000000000000000000.300000000000000000000000000000000000000.00000000000000000000000000000000000000 │
└─────────────────────────────┴──────────────────────────────┴──────────────────────────────────────────┴──────────────────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toString

函数解析:

1、这些函数用于在数字、字符串(不包含FixedString)、Date以及DateTime之间互相转换。所有的函数都接受一个参数。
2、当将其他类型转换到字符串或从字符串转换到其他类型时,使用与TabSeparated格式相同的规则对字符串的值进行格式化或解析。如果无法解析字符串则抛出异常并取消查询。
3、当将Date转换为数字或反之,Date对应Unix时间戳的天数。将DataTime转换为数字或反之,DateTime对应Unix时间戳的秒数。toDate/toDateTime函数的日期和日期时间格式定义如下:
YYYY-MM-DD
YYYY-MM-DD hh:mm:ss
4、如果将UInt32Int32UInt64Int64类型的数值转换为Date类型,并且其对应的值大于等于65536,则该数值将被解析成unix时间戳(而不是对应的天数)。这意味着允许写入'toDate(unix_timestamp)'这种常见情况,否则这将是错误的,并且需要便携更加繁琐的'toDate(toDateTime(unix_timestamp))'
5、DateDateTime之间的转换以更为自然的方式进行:通过添加空的time或删除time
6、数值类型之间的转换与C++中不同数字类型之间的赋值相同的规则。

函数实例:

此外,DateTime参数的toString函数可以在第二个参数中包含时区名称。 例如:Asia/Yekaterinburg在这种情况下,时间根据指定的时区进行格式化。

localhost :) select toString(1),toString('dsafsdf'),toString(toDate('2021-05-21')),toString(toDateTime('2021-05-21 14:17:25')),toString(Null),toString(now(), 'Asia/Yekaterinburg')

SELECT
    toString(1),
    toString('dsafsdf'),
    toString(toDate('2021-05-21')),
    toString(toDateTime('2021-05-21 14:17:25')),
    toString(NULL),
    toString(now(), 'Asia/Yekaterinburg')

Query id: e8766305-4c9e-4446-8001-bc1d3eadaeee

┌─toString(1)─┬─toString('dsafsdf')─┬─toString(toDate('2021-05-21'))─┬─toString(toDateTime('2021-05-21 14:17:25'))─┬─toString(NULL)─┬─toString(now(), 'Asia/Yekaterinburg')─┐
│ 1           │ dsafsdf             │ 2021-05-212021-05-21 14:17:25                         │ ᴺᵁᴸᴸ           │ 2021-05-21 12:04:00                   │
└─────────────┴─────────────────────┴────────────────────────────────┴─────────────────────────────────────────────┴────────────────┴───────────────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

另请参阅toUnixTimestamp函数。

toFixedString(s,N)

函数说明:
  • 作用:
    String类型的参数转换为FixedString(N)类型的值(具有固定长度N的字符串)。
  • 参数:
    s可以为任何形式的字符串。 N必须是一个常量。
  • 返回值:
    如果字符串的字节数少于N,则向右填充空字节。 如果字符串的字节数多于N,则抛出异常。
函数实例:
localhost :) SELECT toFixedString('1',300)

SELECT toFixedString('1', 300)

Query id: 6d56c76c-98bb-4ef5-a327-4b5be4dc37ee

┌─toFixedString('1', 300)─┐
│ 1                       │
└─────────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

localhost :) SELECT toFixedString('dsfs',2)

SELECT toFixedString('dsfs', 2)

Query id: 6a5da263-ec96-4a59-925c-97e9cac9d54e


Received exception from server (version 20.11.3):
Code: 131. DB::Exception: Received from xx.xx.xx.xxx:xxxx. DB::Exception: String too long for type FixedString(2): While processing toFixedString('dsfs', 2).

0 rows in set. Elapsed: 0.003 sec.

toStringCutToZero(s)

函数说明:
  • 参数:
    接受StringFixedString参数。
  • 返回值:
    返回String,其内容在找到的第一个零字节处被截断。
函数实例:

localhost :) SELECT toFixedString('foo', 8) AS s, toStringCutToZero(s) AS s_cut,toFixedString('foo\0bar', 8) AS s1, toStringCutToZero(s1) AS s_cut_1,toFixedString('foofoobar', 10) AS s2, toStringCutToZero(s2) AS s_cut_2

SELECT
    toFixedString('foo', 8) AS s,
    toStringCutToZero(s) AS s_cut,
    toFixedString('foo\0bar', 8) AS s1,
    toStringCutToZero(s1) AS s_cut_1,
    toFixedString('foofoobar', 10) AS s2,
    toStringCutToZero(s2) AS s_cut_2

Query id: 97fb0259-9533-4c90-91a6-85139aaf9e0c

┌─s───┬─s_cut─┬─s1─────┬─s_cut_1─┬─s2────────┬─s_cut_2───┐
│ foo │ foo   │ foobar │ foo     │ foofoobar │ foofoobar │
└─────┴───────┴────────┴─────────┴───────────┴───────────┘

1 rows in set. Elapsed: 0.003 sec.

reinterpretAsUInt(8|16|32|64)

函数说明:

返回传入字符的ASCII编码。

reinterpretAsUInt8(s) -- 参数为字符串,返回值为UInt8的数据类型
reinterpretAsUInt16(s) --参数为字符串,返回值为UInt16的数据类型
reinterpretAsUInt32(s) --参数为字符串,返回值为UInt32的数据类型
reinterpretAsUInt64(s) --参数为字符串,返回值为UInt64的数据类型
  • 参数:
    该参数s为字符串。这些函数接受一个字符串,并将放在字符串开头的字节解释为主机顺序中的数字(little endian)。
  • 返回值:
    1、如果字符串不够长,则函数就像使用必要数量的空字节填充字符串一样。
    2、如果字符串比需要的长,则忽略额外的字节。
    3、返回值为无符号整型。
函数实例:
localhost :) select reinterpretAsUInt8('10000'),reinterpretAsUInt16('10000'),reinterpretAsUInt32('1000000'),reinterpretAsUInt64('-10000000'),reinterpretAsUInt64('10000000'),reinterpretAsUInt8('A'),reinterpretAsUInt8('a')

SELECT
    reinterpretAsUInt8('10000'),
    reinterpretAsUInt16('10000'),
    reinterpretAsUInt32('1000000'),
    reinterpretAsUInt64('-10000000'),
    reinterpretAsUInt64('10000000'),
    reinterpretAsUInt8('A'),
    reinterpretAsUInt8('a')

Query id: 9d8ef3b6-7bf5-4621-9c2b-e483a9b49e7e

┌─reinterpretAsUInt8('10000')─┬─reinterpretAsUInt16('10000')─┬─reinterpretAsUInt32('1000000')─┬─reinterpretAsUInt64('-10000000')─┬─reinterpretAsUInt64('10000000')─┬─reinterpretAsUInt8('A')─┬─reinterpretAsUInt8('a')─┐
│                          4912337808464433347232829622768055734723282962276803056597 │
└─────────────────────────────┴──────────────────────────────┴────────────────────────────────┴──────────────────────────────────┴─────────────────────────────────┴─────────────────────────┴─────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

reinterpretAsInt(8|16|32|64)

函数说明:
reinterpretAsInt8(s) -- 参数为字符串,返回值为Int8的数据类型
reinterpretAsInt16(s) --参数为字符串,返回值为Int16的数据类型
reinterpretAsInt32(s) --参数为字符串,返回值为Int32的数据类型
reinterpretAsInt64(s) --参数为字符串,返回值为Int64的数据类型
  • 参数:
    该参数s为字符串。这些函数接受一个字符串,并将放在字符串开头的字节解释为主机顺序中的数字(little endian)。
  • 返回值:
    1、如果字符串不够长,则函数就像使用必要数量的空字节填充字符串一样。
    2、如果字符串比需要的长,则忽略额外的字节。
    3、返回值为整型。
函数实例:
localhost :) select reinterpretAsInt8('10000'),reinterpretAsInt16('10000'),reinterpretAsInt32('1000000'),reinterpretAsInt64('-10000000'),reinterpretAsInt64('10000000'),reinterpretAsInt8('A'),reinterpretAsInt8('-a')

SELECT
    reinterpretAsInt8('10000'),
    reinterpretAsInt16('10000'),
    reinterpretAsInt32('1000000'),
    reinterpretAsInt64('-10000000'),
    reinterpretAsInt64('10000000'),
    reinterpretAsInt8('A'),
    reinterpretAsInt8('-a')

Query id: 5f3441f2-6735-443a-9c28-4e7003d099f5

┌─reinterpretAsInt8('10000')─┬─reinterpretAsInt16('10000')─┬─reinterpretAsInt32('1000000')─┬─reinterpretAsInt64('-10000000')─┬─reinterpretAsInt64('10000000')─┬─reinterpretAsInt8('A')─┬─reinterpretAsInt8('-a')─┐
│                         4912337808464433347232829622768055734723282962276803056545 │
└────────────────────────────┴─────────────────────────────┴───────────────────────────────┴─────────────────────────────────┴────────────────────────────────┴────────────────────────┴─────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

reinterpretAsFloat(32|64)

reinterpretAsFloat32(s) -- 参数为字符串,返回值为Float32的数据类型
reinterpretAsFloat64(s) --参数为字符串,返回值为Float64的数据类型
  • 参数:
    该参数s为字符串。这些函数接受一个字符串,并将放在字符串开头的字节解释为主机顺序中的数字(little endian)。
  • 返回值:
    1、如果字符串不够长,则函数就像使用必要数量的空字节填充字符串一样。
    2、如果字符串比需要的长,则忽略额外的字节。
    3、返回值为Float32或者Float64类型。
函数实例:
localhost :) select reinterpretAsFloat32('1.111232323232'),reinterpretAsFloat64('1.22232423232'),reinterpretAsFloat32('a'),reinterpretAsFloat64('a')

SELECT
    reinterpretAsFloat32('1.111232323232'),
    reinterpretAsFloat64('1.22232423232'),
    reinterpretAsFloat32('a'),
    reinterpretAsFloat64('a')

Query id: e05aac57-35d6-46a2-8123-a0c2461f8311

┌─reinterpretAsFloat32('1.111232323232')─┬─reinterpretAsFloat64('1.22232423232')─┬─reinterpretAsFloat32('a')─┬─reinterpretAsFloat64('a')─┐
│                           2.5783147e-92.899424837862335e-571.36e-434.8e-322 │
└────────────────────────────────────────┴───────────────────────────────────────┴───────────────────────────┴───────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

reinterpretAsDate (s)

函数说明:
  • 参数:
    该参数s为字符串。这些函数接受一个字符串,并将放在字符串开头的字节解释为主机顺序中的数字(little endian)。
  • 返回值:
    1、如果字符串不够长,则函数就像使用必要数量的空字节填充字符串一样。
    2、如果字符串比需要的长,则忽略额外的字节。
    3、返回值为Date类型。Date被解释为Unix时间戳的天数
函数实例:
localhost :) select reinterpretAsDate('2021-05-21 10:10:10'),reinterpretAsDate('2021-05-21'),reinterpretAsDate('2'),reinterpretAsDate('A')

SELECT
    reinterpretAsDate('2021-05-21 10:10:10'),
    reinterpretAsDate('2021-05-21'),
    reinterpretAsDate('2'),
    reinterpretAsDate('A')

Query id: 89a37b67-9c61-4dbd-8729-57031d4bcb83

┌─reinterpretAsDate('2021-05-21 10:10:10')─┬─reinterpretAsDate('2021-05-21')─┬─reinterpretAsDate('2')─┬─reinterpretAsDate('A')─┐
│                               2003-10-132003-10-131970-02-201970-03-07 │
└──────────────────────────────────────────┴─────────────────────────────────┴────────────────────────┴────────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

reinterpretAsDateTime (s)

函数说明:
  • 参数:
    该参数s为字符串,并将放在字符串开头的字节解释为主机顺序中的数字(little endian)。
  • 返回值:
    1、如果字符串不够长,则函数就像使用必要数量的空字节填充字符串一样。
    2、如果字符串比需要的长,则忽略额外的字节。
    3、返回值为DateTime类型。DateTime被解释为Unix时间戳。
函数实例:
localhost :) select reinterpretAsDateTime('2021-05-21 10:10:10'),reinterpretAsDateTime('2021-05-21'),reinterpretAsDateTime('2'),reinterpretAsDateTime('A')

SELECT
    reinterpretAsDateTime('2021-05-21 10:10:10'),
    reinterpretAsDateTime('2021-05-21'),
    reinterpretAsDateTime('2'),
    reinterpretAsDateTime('A')

Query id: 4a2c0068-836b-4946-b61c-64ce5b359afb

┌─reinterpretAsDateTime('2021-05-21 10:10:10')─┬─reinterpretAsDateTime('2021-05-21')─┬─reinterpretAsDateTime('2')─┬─reinterpretAsDateTime('A')─┐
│                          1996-02-27 06:12:021996-02-27 06:12:021970-01-01 08:00:501970-01-01 08:01:05 │
└──────────────────────────────────────────────┴─────────────────────────────────────┴────────────────────────────┴────────────────────────────┘

1 rows in set. Elapsed: 0.007 sec.

reinterpretAsString(s)

函数说明:
  • 参数:
    该参数s为数字、DateDateTime,并返回一个字符串,其中包含表示主机顺序(小端)的相应值的字节。从末尾删除空字节。例如,UInt32类型值255是一个字节长的字符串。
  • 返回值:
    返回值为String类型。
函数实例:
localhost :) SELECT reinterpretAsString('2021-05-21 10:10:10'),reinterpretAsString('2021-05-21'),reinterpretAsString(toDateTime('2021-05-21 10:10:10')),reinterpretAsString(toDate('2021-05-21')),reinterpretAsString(1),reinterpretAsString(NULL),reinterpretAsString('A'),reinterpretAsString('@')

SELECT
    reinterpretAsString('2021-05-21 10:10:10'),
    reinterpretAsString('2021-05-21'),
    reinterpretAsString(toDateTime('2021-05-21 10:10:10')),
    reinterpretAsString(toDate('2021-05-21')),
    reinterpretAsString(1),
    reinterpretAsString(NULL),
    reinterpretAsString('A'),
    reinterpretAsString('@')

Query id: 9993c0e0-192a-458a-9278-690e724070b1

┌─reinterpretAsString('2021-05-21 10:10:10')─┬─reinterpretAsString('2021-05-21')─┬─reinterpretAsString(toDateTime('2021-05-21 10:10:10'))─┬─reinterpretAsString(toDate('2021-05-21'))─┬─reinterpretAsString(1)─┬─reinterpretAsString(NULL)─┬─reinterpretAsString('A')─┬─reinterpretAsString('@')─┐
│ 2021-05-21 10:10:102021-05-21                        │ ��`                                                      │ PI                                        │                        │ ᴺᵁᴸᴸ                      │ A                        │ @                        │
└────────────────────────────────────────────┴───────────────────────────────────┴────────────────────────────────────────────────────────┴───────────────────────────────────────────┴────────────────────────┴───────────────────────────┴──────────────────────────┴──────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

reinterpretAsFixedString (s)

函数说明:
  • 参数:
    该参数s为数字、DateDateTime,并返回包含表示主机顺序(小端)的相应值的字节的FixedString。从末尾删除空字节。例如,UInt32类型值255是一个长度为一个字节的FixedString
  • 返回值:
    返回值为FixedString类型。
函数实例:
localhost :) SELECT reinterpretAsFixedString(1),reinterpretAsFixedString(101),reinterpretAsFixedString(toDate('2021-05-21')),reinterpretAsFixedString(toDateTime('2021-05-21 10:10:10'))

SELECT
    reinterpretAsFixedString(1),
    reinterpretAsFixedString(101),
    reinterpretAsFixedString(toDate('2021-05-21')),
    reinterpretAsFixedString(toDateTime('2021-05-21 10:10:10'))

Query id: 6d8fb88b-9c46-48c9-be52-dede874ebedc

┌─reinterpretAsFixedString(1)─┬─reinterpretAsFixedString(101)─┬─reinterpretAsFixedString(toDate('2021-05-21'))─┬─reinterpretAsFixedString(toDateTime('2021-05-21 10:10:10'))─┐
│                             │ e                             │ PI                                             │ ��`                                                           │
└─────────────────────────────┴───────────────────────────────┴────────────────────────────────────────────────┴─────────────────────────────────────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

CAST(x, T)

函数说明:

x转换为t数据类型。还支持语法CAST(x AS t)

  • 参数:
    将参数转换为FixedString(N),仅适用于StringFixedString(N)类型的参数。
函数实例:
localhost :) SELECT  '2016-06-15 23:00:00' AS timestamp,CAST(timestamp AS DateTime) AS datetime,CAST(timestamp AS Date) AS date,CAST(timestamp, 'String') AS string,CAST(timestamp, 'FixedString(22)') AS fixed_string

SELECT
    '2016-06-15 23:00:00' AS timestamp,
    CAST(timestamp, 'DateTime') AS datetime,
    CAST(timestamp, 'Date') AS date,
    CAST(timestamp, 'String') AS string,
    CAST(timestamp, 'FixedString(22)') AS fixed_string

Query id: ccfbc4d7-44b2-4815-9a61-e27ccad065ed

┌─timestamp───────────┬────────────datetime─┬───────date─┬─string──────────────┬─fixed_string────────┐
│ 2016-06-15 23:00:002016-06-15 23:00:002016-06-152016-06-15 23:00:002016-06-15 23:00:00 │
└─────────────────────┴─────────────────────┴────────────┴─────────────────────┴─────────────────────┘

1 rows in set. Elapsed: 0.004 sec.

支持将数据转换为可为空。例如:

localhost :) SELECT toTypeName(1),toTypeName(CAST(1, 'Nullable(UInt16)'))

SELECT
    toTypeName(1),
    toTypeName(CAST(1, 'Nullable(UInt16)'))

Query id: 99febea3-e4bf-41d4-87d9-67c067be3746

┌─toTypeName(1)─┬─toTypeName(CAST(1, 'Nullable(UInt16)'))─┐
│ UInt8         │ Nullable(UInt16)                        │
└───────────────┴─────────────────────────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

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

函数说明:

把一个数值类型的值转换为Interval类型的数据。

  • 语法:
    toIntervalSecond(number)
    toIntervalMinute(number)
    toIntervalHour(number)
    toIntervalDay(number)
    toIntervalWeek(number)
    toIntervalMonth(number)
    toIntervalQuarter(number)
    toIntervalYear(number)
  • 参数:
    number — 正整数,持续的时间。
  • 返回值
    时间的Interval值。
函数实例:
localhost :) WITH toDate('2019-01-01') AS date,
:-]      toDateTime('2019-01-01 00:00:00') AS datetime,
:-]      INTERVAL 1 WEEK AS interval_week,
:-]      toIntervalWeek(1) AS interval_to_week,
:-]      INTERVAL 1 YEAR AS interval_year,
:-]      toIntervalYear(1) AS interval_to_year,
:-]      INTERVAL 1 DAY AS interval_day,
:-]      toIntervalDay(1) AS interval_to_day,
:-]      INTERVAL 1 MONTH AS interval_month,
:-]      toIntervalMonth(1) AS interval_to_month,
:-]      INTERVAL 1 QUARTER AS interval_quarter,
:-]      toIntervalQuarter(1) AS interval_to_quarter,
:-]      INTERVAL 1 HOUR AS interval_hour,
:-]      toIntervalHour(1) AS interval_to_hour,
:-]      INTERVAL 1 MINUTE AS interval_mintue,
:-]      toIntervalMinute(1) AS interval_to_minute,
:-]      INTERVAL 1 SECOND AS interval_second,
:-]      toIntervalSecond(1) AS interval_to_second
:-] SELECT
:-]     date + interval_day,
:-]     date + interval_to_day,
:-]     date + interval_week,
:-]     date + interval_to_week,
:-]     date + interval_month,
:-]     date + interval_to_month,
:-]     date + interval_quarter,
:-]     date + interval_to_quarter,
:-]     date + interval_year,
:-]     date + interval_to_year,
:-]     datetime + interval_second,
:-]     datetime + interval_to_second,
:-]     datetime + interval_mintue,
:-]     datetime + interval_to_minute,
:-]     datetime + interval_hour,
:-]     datetime + interval_to_hour;

WITH
    toDate('2019-01-01') AS date,
    toDateTime('2019-01-01 00:00:00') AS datetime,
    toIntervalWeek(1) AS interval_week,
    toIntervalWeek(1) AS interval_to_week,
    toIntervalYear(1) AS interval_year,
    toIntervalYear(1) AS interval_to_year,
    toIntervalDay(1) AS interval_day,
    toIntervalDay(1) AS interval_to_day,
    toIntervalMonth(1) AS interval_month,
    toIntervalMonth(1) AS interval_to_month,
    toIntervalQuarter(1) AS interval_quarter,
    toIntervalQuarter(1) AS interval_to_quarter,
    toIntervalHour(1) AS interval_hour,
    toIntervalHour(1) AS interval_to_hour,
    toIntervalMinute(1) AS interval_mintue,
    toIntervalMinute(1) AS interval_to_minute,
    toIntervalSecond(1) AS interval_second,
    toIntervalSecond(1) AS interval_to_second
SELECT
    date + interval_day,
    date + interval_to_day,
    date + interval_week,
    date + interval_to_week,
    date + interval_month,
    date + interval_to_month,
    date + interval_quarter,
    date + interval_to_quarter,
    date + interval_year,
    date + interval_to_year,
    datetime + interval_second,
    datetime + interval_to_second,
    datetime + interval_mintue,
    datetime + interval_to_minute,
    datetime + interval_hour,
    datetime + interval_to_hour

Query id: b1e72290-5c6d-4f1d-a6b7-5753764413c1

┌─plus(date, interval_day)─┬─plus(date, interval_to_day)─┬─plus(date, interval_week)─┬─plus(date, interval_to_week)─┬─plus(date, interval_month)─┬─plus(date, interval_to_month)─┬─plus(date, interval_quarter)─┬─plus(date, interval_to_quarter)─┬─plus(date, interval_year)─┬─plus(date, interval_to_year)─┬─plus(datetime, interval_second)─┬─plus(datetime, interval_to_second)─┬─plus(datetime, interval_mintue)─┬─plus(datetime, interval_to_minute)─┬─plus(datetime, interval_hour)─┬─plus(datetime, interval_to_hour)─┐
│               2019-01-022019-01-022019-01-082019-01-082019-02-012019-02-012019-04-012019-04-012020-01-012020-01-012019-01-01 00:00:012019-01-01 00:00:012019-01-01 00:01:002019-01-01 00:01:002019-01-01 01:00:002019-01-01 01:00:00 │
└──────────────────────────┴─────────────────────────────┴───────────────────────────┴──────────────────────────────┴────────────────────────────┴───────────────────────────────┴──────────────────────────────┴─────────────────────────────────┴───────────────────────────┴──────────────────────────────┴─────────────────────────────────┴────────────────────────────────────┴─────────────────────────────────┴────────────────────────────────────┴───────────────────────────────┴──────────────────────────────────┘

1 rows in set. Elapsed: 0.008 sec.

parseDateTimeBestEffort

函数说明:
  • 作用:
    1、把String类型的时间日期转换为DateTime数据类型
    2、该函数可以解析ISO 8601RFC 1123 - 5.2.14 RFC-822 Date and Time Specification或者ClickHouse的一些别的时间日期格式
    -语法:
    parseDateTimeBestEffort(time_string [, time_zone]);
  • 参数:
    time_string — 字符类型的时间和日期。
    time_zone — 字符类型的时区。
  • 非标准格式的支持:
    1、9位或者10位的数字时间,unix timestamp
    2、时间和日期组成的字符串:·YYYYMMDDhhmmss, DD/MM/YYYY hh:mm:ss, DD-MM-YY hh:mm, YYYY-MM-DD hh:mm:ss等。
    3、只有日期的字符串: YYYY, YYYYMM, YYYY*MM, DD/MM/YYYY, DD-MM-YY 等。
    4、只有天和时间: DD, DD hh, DD hh:mm。这种情况下YYYY-MM默认为 2000-01
    5、包含时间日期以及时区信息: YYYY-MM-DD hh:mm:ss ±h:mm等。例如: 2020-12-12 17:36:00 -5:00

对于所有的格式来说,这个函数通过全称或者第一个三个字符的月份名称来解析月份,比如:24/DEC/18, 24-Dec-18, 01-September-2018

  • 返回值:
    DateTime类型数据。
函数实例:
localhost :) SELECT parseDateTimeBestEffort('1621588348'),parseDateTimeBestEffort('21/05/2021 17:12:28'),parseDateTimeBestEffort('2021-05-21 17:12:28'),parseDateTimeBestEffort('21-05-2021 17:12:38'),parseDateTimeBestEffort('21-05-2021 17:12'),parseDateTimeBestEffort('2021'),parseDateTimeBestEffort('202105'),parseDateTimeBestEffort('2021*05'),parseDateTimeBestEffort('21/05/2021'),parseDateTimeBestEffort('21-05-2021'),parseDateTimeBestEffort('21'),parseDateTimeBestEffort('21 17'),parseDateTimeBestEffort('21 17:12'),parseDateTimeBestEffort('2020-05-21 17:36:00 -1:00'),parseDateTimeBestEffort('2020-05-21 17:36:00 +1:00'),parseDateTimeBestEffort('Fri, 21 May 2021 17:12:28 GMT','Asia/Shanghai'),parseDateTimeBestEffort('21/May/21'),parseDateTimeBestEffort('21-May-21'),parseDateTimeBestEffort('21-May-2021')

SELECT
    parseDateTimeBestEffort('1621588348'),
    parseDateTimeBestEffort('21/05/2021 17:12:28'),
    parseDateTimeBestEffort('2021-05-21 17:12:28'),
    parseDateTimeBestEffort('21-05-2021 17:12:38'),
    parseDateTimeBestEffort('21-05-2021 17:12'),
    parseDateTimeBestEffort('2021'),
    parseDateTimeBestEffort('202105'),
    parseDateTimeBestEffort('2021*05'),
    parseDateTimeBestEffort('21/05/2021'),
    parseDateTimeBestEffort('21-05-2021'),
    parseDateTimeBestEffort('21'),
    parseDateTimeBestEffort('21 17'),
    parseDateTimeBestEffort('21 17:12'),
    parseDateTimeBestEffort('2020-05-21 17:36:00 -1:00'),
    parseDateTimeBestEffort('2020-05-21 17:36:00 +1:00'),
    parseDateTimeBestEffort('Fri, 21 May 2021 17:12:28 GMT', 'Asia/Shanghai'),
    parseDateTimeBestEffort('21/May/21'),
    parseDateTimeBestEffort('21-May-21'),
    parseDateTimeBestEffort('21-May-2021')

Query id: 84287a72-02f4-40dc-b07f-516399d74b41

┌─parseDateTimeBestEffort('1621588348')─┬─parseDateTimeBestEffort('21/05/2021 17:12:28')─┬─parseDateTimeBestEffort('2021-05-21 17:12:28')─┬─parseDateTimeBestEffort('21-05-2021 17:12:38')─┬─parseDateTimeBestEffort('21-05-2021 17:12')─┬─parseDateTimeBestEffort('2021')─┬─parseDateTimeBestEffort('202105')─┬─parseDateTimeBestEffort('2021*05')─┬─parseDateTimeBestEffort('21/05/2021')─┬─parseDateTimeBestEffort('21-05-2021')─┬─parseDateTimeBestEffort('21')─┬─parseDateTimeBestEffort('21 17')─┬─parseDateTimeBestEffort('21 17:12')─┬─parseDateTimeBestEffort('2020-05-21 17:36:00 -1:00')─┬─parseDateTimeBestEffort('2020-05-21 17:36:00 +1:00')─┬─parseDateTimeBestEffort('Fri, 21 May 2021 17:12:28 GMT', 'Asia/Shanghai')─┬─parseDateTimeBestEffort('21/May/21')─┬─parseDateTimeBestEffort('21-May-21')─┬─parseDateTimeBestEffort('21-May-2021')─┐
│                   2021-05-21 17:12:282021-05-21 17:12:282021-05-21 17:12:282021-05-21 17:12:382021-05-21 17:12:002021-01-01 00:00:002021-05-01 00:00:002021-05-01 00:00:002021-05-21 00:00:002021-05-21 00:00:002000-01-21 00:00:002000-01-21 17:00:002000-01-21 17:12:002020-05-22 02:36:002020-05-22 00:36:002021-05-22 01:12:282021-05-21 00:00:002021-05-21 00:00:002021-05-21 00:00:00 │
└───────────────────────────────────────┴────────────────────────────────────────────────┴────────────────────────────────────────────────┴────────────────────────────────────────────────┴─────────────────────────────────────────────┴─────────────────────────────────┴───────────────────────────────────┴────────────────────────────────────┴───────────────────────────────────────┴───────────────────────────────────────┴───────────────────────────────┴──────────────────────────────────┴─────────────────────────────────────┴──────────────────────────────────────────────────────┴──────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────┴──────────────────────────────────────┴────────────────────────────────────────┘

1 rows in set. Elapsed: 0.020 sec.

parseDateTimeBestEffortOrNull

函数说明:

这个函数和parseDateTimeBestEffort基本一致,除了无法解析返回结果为NULL

函数实例:
localhost :) select parseDateTimeBestEffortOrNull('a')

SELECT parseDateTimeBestEffortOrNull('a')

Query id: d0acedc7-a8cb-4b53-8bce-fed8c0c5279b

┌─parseDateTimeBestEffortOrNull('a')─┐
│                               ᴺᵁᴸᴸ │
└────────────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

parseDateTimeBestEffortOrZero

函数说明:

这个函数和parseDateTimeBestEffort基本一致,除了无法解析返回结果为0

函数实例:
localhost :) select parseDateTimeBestEffortOrZero('a')

SELECT parseDateTimeBestEffortOrZero('a')

Query id: accf8339-01a7-47aa-b5f0-b6f8709ec908

┌─parseDateTimeBestEffortOrZero('a')─┐
│                1970-01-01 08:00:00 │
└────────────────────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

toLowCardinality

函数说明:
  • 作用:
    把输入值转换为LowCardianlity的相同类型的数据。
    如果要把LowCardinality类型的数据转换为其他类型,使用CAST函数。比如:CAST(x as String)
  • 语法:
    toLowCardinality(expr)
  • 参数:
    expr — 表达式为支持的数据类型的一种。
  • 返回值:
    expr的结果。
  • 类型:
    LowCardinality(expr_result_type)
函数实例:
localhost :) SELECT toLowCardinality('1'),toLowCardinality(1),toLowCardinality(1.11),toLowCardinality('2021-05-21'),toLowCardinality('2021-05-21 17:40:33')

SELECT
    toLowCardinality('1'),
    toLowCardinality(1),
    toLowCardinality(1.11),
    toLowCardinality('2021-05-21'),
    toLowCardinality('2021-05-21 17:40:33')

Query id: 0ea67a4c-4427-4173-a712-428483bf50b1

┌─toLowCardinality('1')─┬─toLowCardinality(1)─┬─toLowCardinality(1.11)─┬─toLowCardinality('2021-05-21')─┬─toLowCardinality('2021-05-21 17:40:33')─┐
│ 111.112021-05-212021-05-21 17:40:33                     │
└───────────────────────┴─────────────────────┴────────────────────────┴────────────────────────────────┴─────────────────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

toUnixTimestamp64Milli、toUnixTimestamp64Micro、toUnixTimestamp64Nano

函数说明:
  • 作用:
    把一个DateTime64类型的数据转换为Int64类型的数据,结果包含固定亚秒的精度。输入的值是变大还是变低依赖于输入的精度。需要注意的是输出的值是一个UTC的时间戳,
    不是同一个时区的DateTime64值。
  • 语法:
    toUnixTimestamp64Milli(value)
  • 参数:
    value — 任何精度的DateTime64类型的数据。
  • 返回值:
    value Int64类型数据。
函数实例:
localhost :) WITH toDateTime64('2019-09-16 19:20:12.345678910', 6) AS dt64 SELECT toUnixTimestamp64Milli(dt64),toUnixTimestamp64Nano(dt64),toUnixTimestamp64Micro(dt64)

WITH toDateTime64('2019-09-16 19:20:12.345678910', 6) AS dt64
SELECT
    toUnixTimestamp64Milli(dt64),
    toUnixTimestamp64Nano(dt64),
    toUnixTimestamp64Micro(dt64)

Query id: dd7580e8-d121-4464-8aef-2460d3cdf527

┌─toUnixTimestamp64Milli(dt64)─┬─toUnixTimestamp64Nano(dt64)─┬─toUnixTimestamp64Micro(dt64)─┐
│                156863281234515686328123456780001568632812345678 │
└──────────────────────────────┴─────────────────────────────┴──────────────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

fromUnixTimestamp64Milli、fromUnixTimestamp64Micro、fromUnixTimestamp64Nano

函数说明:
  • 作用:
    Int64类型的数据转换为DateTime64类型的数据,结果包含固定的亚秒精度和可选的时区。
    输入的值是变大还是变低依赖于输入的精度。需要注意的是输入的值是一个UTC的时间戳, 不是一个包含时区的时间戳。
  • 语法:
    fromUnixTimestamp64Milli(value [, ti])
  • ** 参数:**
    valueInt64类型的数据,可以是任意精度。
    timezoneString类型的时区
  • 返回值:
    value DateTime64类型的数据。
函数实例:
localhost :) WITH CAST(1234567891011, 'Int64') AS i64 SELECT fromUnixTimestamp64Milli(i64, 'UTC'),fromUnixTimestamp64Micro(i64,'UTC'),fromUnixTimestamp64Nano(i64, 'UTC')

WITH CAST(1234567891011, 'Int64') AS i64
SELECT
    fromUnixTimestamp64Milli(i64, 'UTC'),
    fromUnixTimestamp64Micro(i64, 'UTC'),
    fromUnixTimestamp64Nano(i64, 'UTC')

Query id: 5d6e124d-1c90-4152-b856-d66a51d1ab11

┌─fromUnixTimestamp64Milli(i64, 'UTC')─┬─fromUnixTimestamp64Micro(i64, 'UTC')─┬─fromUnixTimestamp64Nano(i64, 'UTC')─┐
│              2009-02-13 23:31:31.0111970-01-15 06:56:07.8910111970-01-01 00:20:34.567891011 │
└──────────────────────────────────────┴──────────────────────────────────────┴─────────────────────────────────────┘

1 rows in set. Elapsed: 0.003 sec.

结语

到此类型准换函数基本一一介绍完毕,希望对大家有帮助。

  • 10
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
ClickHouse中,有多个类型转换函数可用于将数据从一种类型转换为另一种类型。其中一些函数包括: 1. 常规类型转换: 这些函数将一个类型转换为另一个常规类型。例如,toInt8、toInt16、toInt32、toInt64、toFloat32、toFloat64等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [clickhouse 数据类型转换](https://blog.csdn.net/weixin_38424594/article/details/123545926)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Clickhouse 类型转换函数实战总结](https://blog.csdn.net/qq_41018861/article/details/117087067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [aiochclient:轻量级异步http(s)ClickHouse客户端,适用于python 3.6+,具有类型转换功能](https://download.csdn.net/download/weixin_42110469/18740950)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扫地增

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值