类型转换函数

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

      示例:

    • hextoraw(string)

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

      返回值类型:raw

      示例:

      1. hextoraw
      2. ----------
      3. 7D
      4. (1 row)
    • numtoday(numeric)

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

      返回值类型:timestamp

      示例:

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

      描述:获取系统时间戳。

      返回值类型:timestamp with time zone

      示例:

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

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

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

      返回值类型:varchar

      示例:

      1. postgres=# SELECT rawtohex('1234567');
      2. rawtohex
      3. ----------------
      4. 31323334353637
      5. (1 row)
    • to_char (datetime/interval [, fmt])

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

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

      返回值类型:varchar

      示例:

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

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

      返回值类型:text

      示例:

      1. postgres=# SELECT to_char(125.8::real, '999D99');
      2. to_char
      3. ---------
      4. 125.80
      5. (1 row)
    • to_char (integer/number[, fmt])

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

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

      返回值类型:varchar

      示例:

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

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

      返回值类型:text

      示例:

    • to_char(int, text)

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

      返回值类型:text

      示例:

      1. postgres=# SELECT to_char(125, '999');
      2. to_char
      3. 125
      4. (1 row)
    • to_char(numeric, text)

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

      返回值类型:text

      1. postgres=# 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. postgres=# SELECT to_char('01110');
      2. to_char
      3. ---------
      4. 01110
      5. (1 row)
    • to_char(timestamp, text)

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

      返回值类型:text

      示例:

      1. postgres=# 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/nvarchar2/text/raw)

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

      返回值类型:clob

      示例:

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

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

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

      返回值类型:timestamp

      示例:

    • to_date(text, text)

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

      返回值类型:timestamp

      示例:

      1. postgres=# SELECT to_date('05 Dec 2000', 'DD Mon YYYY');
      2. ---------------------
      3. 2000-12-05 00:00:00
    • to_date(string, fmt)

      描述:

      将字符串string按fmt指定格式转化为DATE类型的值。

      该函数不能直接支持CLOB类型,但是CLOB类型的参数能够通过隐式转换实现。

      返回值类型:date

      示例:

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

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

      类型转换格式请参考表1

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

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

      返回值类型:number

      示例:

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

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

      返回值类型:numeric

      示例:

      1. postgres=# 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. postgres=# SELECT to_timestamp(1284352323);
      2. to_timestamp
      3. ------------------------
      4. 2010-09-13 12:32:03+08
      5. (1 row)
    • 描述:将字符串string按fmt指定的格式转换成时间戳类型的值。不指定fmt时,按参数nls_timestamp_format所指定的格式转换。

      openGauss的to_timestamp中,

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

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

      返回值类型:timestamp without time zone

      示例:

      1. postgres=# SHOW nls_timestamp_format;
      2. nls_timestamp_format
      3. ----------------------------
      4. DD-Mon-YYYY HH:MI:SS.FF AM
      5. (1 row)
      6. postgres=# SELECT to_timestamp('12-sep-2014');
      7. to_timestamp
      8. ---------------------
      9. 2014-09-12 00:00:00
      10. (1 row)
      1. postgres=# 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. postgres=# SELECT to_timestamp('-1','SYYYY');
      2. to_timestamp
      3. ------------------------
      4. 0001-01-01 00:00:00 BC
      5. (1 row)
      1. postgres=# SELECT to_timestamp('98','RR');
      2. to_timestamp
      3. ---------------------
      4. 1998-01-01 00:00:00
      5. (1 row)
      1. postgres=# 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. postgres=# SELECT to_timestamp('05 Dec 2000', 'DD Mon YYYY');
      2. to_timestamp
      3. ---------------------
      4. 2000-12-05 00:00:00
      5. (1 row)

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