云数据库 GAUSSDB-类型转换函数:cast(x as y [DEFAULT z ON CONVERSION ERROR][,fmt])
cast(x as y [DEFAULT z ON CONVERSION ERROR][,fmt])
描述:类型转换函数,将x转换成y指定的类型。当sql_compatibility = 'B'时,设置参数b_format_version = '5.7'、b_format_dev_version = 's1'后,若y是char类型,则该函数会将x转换成varchar类型。
参数:
- x为该函数中待转换的数据,其类型和值需要支持转换为目标类型y。
- DEFAULT z ON CONVERSION ERROR为可选参数,当尝试将x转换成y指定的类型失败时,则将z转换成y指定的类型。
- fmt为可选参数,当y是以下数据类型时可以指定fmt参数:
int1/int2/int4/int8/int16/float4/float8/numeric:则fmt的作用与to_number(expr [,fmt])函数相同。
date/timestamp/timestamp with time zone:则fmt的作用与to_date(string [,fmt])/to_timestamp(string [,fmt]) /to_timestamp_tz(string [,fmt])函数相同。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
gaussdb=# SELECT cast('22-oct-1997' as timestamp); timestamp --------------------- 1997-10-22 00:00:00 (1 row) gaussdb=# SELECT cast('22-ocX-1997' as timestamp DEFAULT '22-oct-1997' ON CONVERSION ERROR, 'DD-Mon-YYYY'); timestamp --------------------- 1997-10-22 00:00:00 (1 row) gaussdb=# CREATE DATABASE gaussdb_m WITH dbcompatibility 'b'; gaussdb=# \c gaussdb_m -- 设置兼容版本控制参数 gaussdb_m=# SET b_format_version='5.7'; gaussdb_m=# SET b_format_dev_version='s1'; gaussdb_m=# SELECT cast('aaa' as char); varchar --------- aaa (1 row) |

在参数a_format_version值为10c且a_format_dev_version值为s1的情况下,支持DEFAULT z ON CONVERSION ERROR及fmt语法。