类型转换函数

    • 描述:类型转换函数,将money转换成text。

      示例:

    • cast(x as y)

      描述:类型转换函数,将x转换成y指定的类型。

      示例:

      1. timestamp
      2. ---------------------
      3. 1997-10-22 00:00:00
      4. (1 row)
    • hextoraw(raw)

      描述:将一个十六进制构成的字符串转换为raw类型。

      返回值类型:raw

      示例:

      1. openGauss=# SELECT hextoraw('7D');
      2. hextoraw
      3. ----------
      4. 7D
      5. (1 row)
    • numtoday(numeric)

      描述:将数字类型的值转换为指定格式的时间戳。

      返回值类型:timestamp

      示例:

      1. openGauss=# SELECT numtoday(2);
      2. numtoday
      3. ----------
      4. 2 days
      5. (1 row)
    • pg_systimestamp()

      描述:获取系统时间戳。

      返回值类型:timestamp with time zone

      示例:

      1. openGauss=# SELECT pg_systimestamp();
      2. pg_systimestamp
      3. -------------------------------
      4. 2015-10-14 11:21:28.317367+08
      5. (1 row)
    • rawtohex(string)

      描述:将一个二进制构成的字符串转换为十六进制的字符串。

      结果为输入字符的ACSII码,以十六进制表示。

      返回值类型:varchar

      示例:

      1. openGauss=# SELECT rawtohex('1234567');
      2. rawtohex
      3. ----------------
      4. 31323334353637
      5. (1 row)
    • to_bigint(varchar)

      描述:将字符类型转换为bigint类型。

      返回值类型:bigint

      示例:

      1. openGauss=# SELECT to_bigint('123364545554455');
      2. to_bigint
      3. ----------------
      4. 123364545554455
      5. (1 row)
    • to_char(datetime/interval [, fmt])

      描述:将一个DATE、TIMESTAMP、TIMESTAMP WITH TIME ZONE或者TIMESTAMP WITH LOCAL TIME ZONE类型的DATETIME或者INTERVAL值按照fmt指定的格式转换为TEXT类型。

      • 可选参数fmt可以为以下几类:日期、时间、星期、季度和世纪。每类都可以有不同的模板,模板之间可以合理组合,常见的模板有:HH、MI、SS、YYYY、MM、DD。
      • 模板可以有修饰词,常用的修饰词是FM,可以用来抑制前导的零或尾随的空白。

      返回值类型:text

      示例:

      1. openGauss=# SELECT to_char(current_timestamp,'HH12:MI:SS');
      2. to_char
      3. ----------
      4. 10:19:26
      5. (1 row)
      1. openGauss=# SELECT to_char(current_timestamp,'FMHH12:FMMI:FMSS');
      2. to_char
      3. ----------
      4. 10:19:46
      5. (1 row)
    • to_char(double precision/real, text)

      描述:将浮点类型的值转换为指定格式的字符串。

      返回值类型:text

      示例:

      1. openGauss=# SELECT to_char(125.8::real, '999D99');
      2. to_char
      3. ---------
      4. 125.80
      5. (1 row)
    • to_char(numeric/smallint/integer/bigint/double precision/real[, fmt])

      描述:将一个整型或者浮点类型的值转换为指定格式的字符串。

      • 可选参数fmt可以为以下几类:十进制字符、“分组”符、正负号和货币符号,每类都可以有不同的模板,模板之间可以合理组合,常见的模板有:9、0、,(千分隔符)、.(小数点)。
      • 模板可以有类似FM的修饰词,但FM不抑制由模板0指定而输出的0。
      • 要将整型类型的值转换成对应16进制值的字符串,使用模板X或x。

      返回值类型:varchar

      示例:

      1. openGauss=# SELECT to_char(1485,'9,999');
      2. to_char
      3. ---------
      4. 1,485
      5. (1 row)
      1. openGauss=# SELECT to_char( 1148.5,'9,999.999');
      2. to_char
      3. ------------
      4. 1,148.500
      5. (1 row)
      1. openGauss=# SELECT to_char(148.5,'990999.909');
      2. to_char
      3. -------------
      4. 0148.500
      5. (1 row)
    • to_char(interval, text)

      描述:将时间间隔类型的值转换为指定格式的字符串。

      返回值类型:text

      示例:

      1. openGauss=# SELECT to_char(interval '15h 2m 12s', 'HH24:MI:SS');
      2. to_char
      3. 15:02:12
      4. (1 row)
    • to_char(int, text)

      描述:将整数类型的值转换为指定格式的字符串。

      返回值类型:text

      示例:

      1. openGauss=# SELECT to_char(125, '999');
      2. to_char
      3. ---------
      4. 125
      5. (1 row)
    • to_char(set)

      描述:将SET类型的值转换为字符串

      返回值:text

      示例:

      1. -- site employeeSET类型的字段
      2. openGauss=# select to_char(site) from employee;
      3. to_char
      4. -----------------
      5. beijing,nanjing
      6. beijing,wuhan
      7. (2 rows)
    • to_char(numeric, text)

      描述:将数字类型的值转换为指定格式的字符串。

      返回值类型:text

      示例:

      1. openGauss=# SELECT to_char(-125.8, '999D99S');
      2. to_char
      3. ---------
      4. 125.80-
      5. (1 row)
    • to_char(string)

      描述:将CHAR、VARCHAR、VARCHAR2、CLOB类型转换为VARCHAR类型。

      如使用该函数对CLOB类型进行转换,且待转换CLOB类型的值超出目标类型的范围,则返回错误。

      返回值类型:varchar

      示例:

      1. openGauss=# SELECT to_char('01110');
      2. to_char
      3. 01110
      4. (1 row)
    • to_char(timestamp, text)

      描述:将时间戳类型的值转换为指定格式的字符串。

      返回值类型:text

      示例:

      1. openGauss=# SELECT to_char(current_timestamp, 'HH12:MI:SS');
      2. to_char
      3. ----------
      4. 10:55:59
      5. (1 row)
    • to_clob(char/nchar/varchar/varchar2/nvarchar/nvarchar2/text/raw)

      描述:将RAW类型或者文本字符集类型CHAR、NCHAR、VARCHAR、VARCHAR2、NVARCHAR、NVARCHAR2、TEXT转成CLOB类型。

      返回值类型:clob

      示例:

      1. openGauss=# SELECT to_clob('ABCDEF'::RAW(10));
      2. to_clob
      3. ---------
      4. ABCDEF
      5. (1 row)
      1. openGauss=# SELECT to_clob('hello111'::CHAR(15));
      2. to_clob
      3. ----------
      4. hello111
      5. (1 row)
      1. openGauss=# SELECT to_clob('gauss123'::NCHAR(10));
      2. to_clob
      3. ----------
      4. gauss123
      5. (1 row)
      1. openGauss=# SELECT to_clob('gauss234'::VARCHAR(10));
      2. to_clob
      3. ----------
      4. gauss234
      5. (1 row)
      1. openGauss=# SELECT to_clob('gauss345'::VARCHAR2(10));
      2. to_clob
      3. ----------
      4. gauss345
      5. (1 row)
      1. openGauss=# SELECT to_clob('gauss456'::NVARCHAR2(10));
      2. to_clob
      3. ----------
      4. gauss456
      5. (1 row)
    • to_date(text)

      描述:将文本类型的值转换为指定格式的时间戳。目前只支持两类格式。

      • 格式一:无分隔符日期,如20150814,需要包括完整的年月日。
      • 格式二:带分隔符日期,如2014-08-14,分隔符可以是单个任意非数字字符。

      返回值类型:timestamp without time zone

      示例:

      1. openGauss=# SELECT to_date('2015-08-14');
      2. to_date
      3. 2015-08-14 00:00:00
      4. (1 row)
    • to_date(text, text)

      描述:将字符串类型的值转换为指定格式的日期。

      返回值类型:timestamp without time zone

      示例:

      1. openGauss=# SELECT to_date('05 Dec 2000', 'DD Mon YYYY');
      2. to_date
      3. ---------------------
      4. 2000-12-05 00:00:00
      5. (1 row)
    • to_number ( expr [, fmt])

      描述:将expr按指定格式转换为一个NUMBER类型的值。

      类型转换格式请参考。

      转换十六进制字符串为十进制数字时,最多支持16个字节的十六进制字符串转换为无符号数。

      转换十六进制字符串为十进制数字时,格式字符串中不允许出现除’x’或’X’以外的其他字符,否则报错。

      返回值类型:number

      示例:

      1. openGauss=# SELECT to_number('12,454.8-', '99G999D9S');
      2. to_number
      3. -----------
      4. -12454.8
      5. (1 row)
    • to_number(text, text)

      描述:将字符串类型的值转换为指定格式的数字。

      返回值类型:numeric

      示例:

      1. openGauss=# SELECT to_number('12,454.8-', '99G999D9S');
      2. to_number
      3. -----------
      4. -12454.8
      5. (1 row)
    • to_timestamp(double precision)

      描述:把UNIX纪元转换成时间戳。

      返回值类型:timestamp with time zone

      示例:

      1. openGauss=# SELECT to_timestamp(1284352323);
      2. to_timestamp
      3. ------------------------
      4. 2010-09-13 12:32:03+08
      5. (1 row)
    • to_timestamp(string [,fmt])

      描述:将字符串string按fmt指定的格式转换成时间戳类型的值。不指定fmt时,按参数nls_timestamp_format所指定的格式转换。

      openGauss的to_timestamp中,

      • 如果输入的年份YYYY=0,系统报错。
      • 如果输入的年份YYYY<0,在fmt中指定SYYYY,则正确输出公元前绝对值n的年份。

      fmt中出现的字符必须与日期/时间格式化的模式相匹配,否则报错。

      返回值类型:timestamp without time zone

      示例:

      1. openGauss=# SHOW nls_timestamp_format;
      2. nls_timestamp_format
      3. ----------------------------
      4. DD-Mon-YYYY HH:MI:SS.FF AM
      5. (1 row)
      6. openGauss=# SELECT to_timestamp('12-sep-2014');
      7. to_timestamp
      8. ---------------------
      9. 2014-09-12 00:00:00
      10. (1 row)
      1. openGauss=# SELECT to_timestamp('12-Sep-10 14:10:10.123000','DD-Mon-YY HH24:MI:SS.FF');
      2. to_timestamp
      3. -------------------------
      4. 2010-09-12 14:10:10.123
      5. (1 row)
      1. openGauss=# SELECT to_timestamp('-1','SYYYY');
      2. to_timestamp
      3. ------------------------
      4. 0001-01-01 00:00:00 BC
      5. (1 row)
      1. openGauss=# SELECT to_timestamp('98','RR');
      2. to_timestamp
      3. ---------------------
      4. 1998-01-01 00:00:00
      5. (1 row)
      1. openGauss=# SELECT to_timestamp('01','RR');
      2. to_timestamp
      3. ---------------------
      4. 2001-01-01 00:00:00
      5. (1 row)
    • to_timestamp(text, text)

      描述:将字符串类型的值转换为指定格式的时间戳。

      返回值类型:timestamp

      示例:

      1. to_timestamp
      2. ---------------------
      3. 2000-12-05 00:00:00
      4. (1 row)

    表 1 数值格式化的模版模式

    • abstime_text

      描述:将abstime类型转为text类型输出。

      参数:abstime

      返回值类型:text

    • abstime_to_smalldatetime

      描述:将abstime类型转为smalldatatime类型。

      参数:abstime

      返回值类型:smalldatetime

    • bigint_tid

      描述:将bigint转为tid。

      参数:bigint

      返回值类型:tid

    • bool_int1

      描述:将bool转为int1。

      参数:boolean

      返回值类型:tinyint

    • bool_int2

      描述:将bool转为int2。

      参数:boolean

      返回值类型:smallint

    • bool_int8

      描述:将bool转为tinyint。

      参数:boolean

      返回值类型:bigint

    • bpchar_date

      描述:将字符串转为日期。

      参数:character

      返回值类型:date

    • bpchar_float4

      描述:将字符串转为float4。

      参数:character

      返回值类型:real

    • bpchar_float8

      描述:将字符串转为float8。

      参数:character

      返回值类型:double precision

    • bpchar_int4

      描述:将字符串转为int4。

      参数:character

      返回值类型:integer

    • bpchar_int8

      参数:character

      返回值类型:bigint

    • bpchar_numeric

      描述:将字符串转为numeric。

      参数:character

      返回值类型:numeric

    • bpchar_timestamp

      描述:将字符串转为时间戳。

      参数:character

      返回值类型:timestamp without time zone

    • bpchar_to_smalldatetime

      描述:将字符串转为smalldatetime。

      参数:character

      返回值类型:smalldatetime

    • cupointer_bigint

      描述:将列存CU指针类型转为bigint类型。

      参数:text

      返回值类型:bigint

    • date_bpchar

      描述:将date类型转换为bpchar类型。

      参数:date

      返回值类型:character

    • date_text

      描述:将date类型转换为text类型。

      参数:date

      返回值类型:text

    • date_varchar

      描述:将date类型转换为varchar类型。

      参数:date

      返回值类型:character varying

    • f4toi1

      描述:把float4类型强转为tinyint unsigned类型。

      参数:real

      返回值类型:tinyint unsigned

    • f8toi1

      描述:把float8类型强转为tinyint unsigned类型。

      参数:double precision

      返回值类型:tinyint unsigned

    • float4_bpchar

      描述:float4转换为bpchar。

      参数:real

      返回值类型:character

    • float4_text

      描述:float4转换为text。

      参数:real

      返回值类型:text

    • float4_varchar

      描述:float4转换为varchar。

      参数:real

      返回值类型:character varying

    • float8_bpchar

      描述:float8转换为bpchar。

      参数:double precision

      返回值类型:character

    • float8_interval

      描述:float8转换为interval。

      参数:double precision

      返回值类型:interval

    • float8_text

      描述:float8转换为text。

      参数:double precision

      返回值类型:text

    • float8_varchar

      描述:float8转换为varchar。

      参数:double precision

      返回值类型:character varying

    • i1tof4

      描述:tinyint unsigned转换为float4。

      参数:tinyint unsigned

      返回值类型:real

    • i1tof8

      描述:tinyint unsigned转换为float8。

      参数:tinyint unsigned

      返回值类型:double precision

    • i1toi2

      描述:tinyint unsigned转换为smallint。

      参数:tinyint unsigned

      返回值类型:smallint

    • i1toi4

      描述:tinyint unsigned转换为int。

      参数:tinyint unsigned

      返回值类型:integer

    • i1toi8

      描述:tinyint unsigned转换为bigint。

      参数:tinyint unsigned

      返回值类型:bigint

    • i2toi1

      描述:smallint转换为tinyint unsigned。

      参数:smallint

      返回值类型:tinyint unsigned

    • i4toi1

      描述:int转换为tinyint unsigned。

      参数:integer

      返回值类型:tinyint unsigned

    • i8toi1

      描述:bigint转换为tinyint unsigned。

      参数:bigint

      返回值类型:tinyint unsigned

    • int1_avg_accum

      描述:将第二个tinyint unsigned类型参数,加入到第一个参数中,一个参数为bigint类型数组。

      参数:bigint[], tinyint unsigned

      返回值类型:bigint[]

    • int1_bool

      描述:tinyint unsigned转换为bool。

      参数:tinyint unsigned

      返回值类型:boolean

    • int1_bpchar

      描述:tinyint unsigned转换为bpchar。

      参数:tinyint unsigned

      返回值类型:character

    • int1_mul_cash

      描述:返回一个tinyint类型参数和一个cash类型参数的乘积,返回值为cash类型。

      参数:tinyint, money

      返回值类型:money

    • int1_numeric

      描述:tinyint unsigned转换为numeric。

      参数:tinyint unsigned

      返回值类型:numeric

    • int1_nvarchar2

      描述:tinyint unsigned转换为nvarchar2。

      参数:tinyint unsigned

      返回值类型:nvarchar2

    • int1_text

      描述:tinyint unsigned转换为text。

      参数:tinyint unsigned

      返回值类型:text

    • int1_varchar

      描述:tinyint unsigned转换为varchar。

      参数:tinyint unsigned

      返回值类型:character varying

    • int1in

      描述:字符串转化为无符号一字节整数。

      参数:cstring

      返回值类型:tinyint

    • int1out

      描述:无符号一字节整数转化为字符串。

      参数:tinyint

      返回值类型:cstring

    • int1up

      描述:输入整数转化为无符号一字节整数。

      参数:tinyint

      返回值类型:tinyint

    • int2_bool

      描述:将有符号二字节整数转化为bool型。

      参数:smallint

      返回值类型:boolean

    • int2_bpchar

      描述:将有符号二字节整数转化为BpChar。

      参数:smallint

      返回值类型:character

    • int2_text

      描述:有符号二字节整数转化为text类型。

      参数:smallint

      返回值类型:text

    • int2_varchar

      描述:有符号二字节整数转化为varchar类型。

      参数:smallint

      返回值类型:character varying

    • int8_text

      描述:tinyint转化为text类型。

      参数:bigint

      返回值类型:text

    • int8_varchar

      描述:tinyint为varchar。

      参数:bigint

      返回值类型:character varying

    • intervaltonum

      描述:将内部数据类型日期转化为numeric类型。

      参数:interval

      返回值类型:numeric

    • numeric_bpchar

      描述:numeric转化为bpchar。

      参数:numeric

      返回值类型:character

    • 描述:numeric转化为有符号1字节整数。

      参数:numeric

      返回值类型:tinyint

    • numeric_text

      描述:numeric转化为text。

      参数:numeric

      返回值类型:text

    • numeric_varchar

      描述:numeric转化为varchar。

      参数:numeric

      返回值类型:character varying

    • nvarchar2in

      描述:将c字符串转化为varchar。

      参数:cstring, oid, integer

      返回值类型:nvarchar2

    • nvarchar2out

      描述:将text转化为c字符串。

      参数:nvarchar2

      返回值类型:cstring

    • nvarchar2send

      描述:将varchar转化为二进制。

      参数:nvarchar2

      返回值类型:bytea

    • oidvectorin_extend

      描述:将字符串转化为oidvector。

      参数:cstring

      返回值类型:oidvector_extend

    • oidvectorout_extend

      描述:将oidvector转化为字符串。

      参数:oidvector_extend

      返回值类型:cstring

    • oidvectorsend_extend

      描述:将oidvector转化为字符串。

      参数:oidvector_extend

      返回值类型:bytea

    • reltime_text

      描述:reltime转换为text。

      参数:reltime

      返回值类型:text

    • text_date

      描述:text类型转换为date类型。

      参数:text

      返回值类型:date

    • text_float4

      描述:text类型转换为float4类型。

      参数:text

      返回值类型:real

    • text_float8

      描述:text类型转换为float8类型。

      参数:text

      返回值类型:double precision

    • text_int1

      描述:text类型转换为int1类型。

      参数:text

      返回值类型:tinyint

    • text_int2

      描述:text类型转换为int2类型。

      参数:text

      返回值类型:smallint

    • text_int4

      描述:text类型转换为int4类型。

      参数:text

      返回值类型:integer

    • text_int8

      描述:text类型转换为tinyint类型。

      参数:text

      返回值类型:bigint

    • text_numeric

      描述:text类型转换为numeric类型。

      参数:text

      返回值类型:numeric

    • text_timestamp

      描述:text类型转换为timestamp类型。

      参数:text

      返回值类型:timestamp without time zone

    • time_text

      描述:time类型转换为text类型。

      参数:time without time zone

      返回值类型:text

    • timestamp_text

      描述:timestamp类型转换为text类型。

      参数:timestamp without time zone

      返回值类型:text

    • timestamp_to_smalldatetime

      描述:timestamp类型转换为smalldatetime类型。

      参数:timestamp without time zone

      返回值类型:smalldatetime

    • timestamp_varchar

      描述:timestamp类型转换为varchar类型。

      参数:timestamp without time zone

      返回值类型:character varying

    • timestamptz_to_smalldatetime

      描述:timestamptz类型转换为smalldatetime。

      参数:timestamp with time zone

      返回值类型:smalldatetime

    • timestampzone_text

      描述:timestampzone类型转换为text类型。

      参数:timestamp with time zone

      返回值类型:text

    • timetz_text

      描述:timetz类型转换为text类型。

      参数:time with time zone

      返回值类型:text

    • to_integer

      描述:转换为integer类型。

      参数:character varying

      返回值类型:integer

    • to_interval

      描述:转换为interval类型。

      参数:character varying

      返回值类型:interval

    • to_numeric

      描述:转换为numeric类型。

      参数:character varying

      返回值类型:numeric

    • to_nvarchar2

      描述:转换为nvarchar2类型。

      参数:numeric

      返回值类型:nvarchar2

    • to_text

      描述:转换为text类型。

      参数:smallint

      返回值类型:text

    • to_ts

      描述:转换为ts类型。

      参数:character varying

      返回值类型:timestamp without time zone

    • to_varchar2

      描述:转换为varchar2类型。

      参数:timestamp without time zone

      返回值类型:character varying

    • varchar_date

      描述:varchar类型转换为date。

      参数:character varying

      返回值类型:date

    • varchar_float4

      描述:varchar类型转换为float4。

      参数:character varying

      返回值类型:real

    • varchar_float8

      描述:varchar类型转换为float8。

      参数:character varying

      返回值类型:double precision

    • varchar_int4

      描述:varchar类型转换为int4。

      参数:character varying

      返回值类型:integer

    • varchar_int8

      描述:varchar类型转换为tinyint。

      参数:character varying

      返回值类型:bigint

    • varchar_numeric

      描述:varchar类型转换为numeric。

      参数:character varying

      返回值类型:numeric

    • varchar_timestamp

      描述:varchar类型转换为timestamp。

      参数:character varying

      返回值类型:timestamp without time zone

    • varchar2_to_smlldatetime

      描述:varchar2类型转换为smlldatetime。

      参数:character varying

      返回值类型:smalldatetime

    • xidout4

      描述:xid输出为4字节数字。

      参数:xid32

      返回值类型:cstring

    • xidsend4

      描述:xid转换为二进制格式。

      参数:xid32

      返回值类型:bytea

    编码类型转换

    • convert_to_nocase(text, text)

      描述:将字符串转换为指定的编码类型。

      返回值类型:bytea

      1. openGauss=# SELECT convert_to_nocase('12345', 'GBK');
      2. convert_to_nocase
      3. -------------------