华为云用户手册

  • statement_timestamp() 描述:当前日期和时间(当前语句的开始)。 返回值类型:timestamp with time zone 示例: 1 2 3 4 5 gaussdb=# SELECT statement_timestamp(); statement_timestamp ------------------------------- 2017-09-01 17:04:39.119267+08 (1 row)
  • last_day(d) 描述:用于计算时间点d当月最后一天的时间。 返回值类型:timestamp 示例: 1 2 3 4 5 gaussdb=# SELECT last_day(to_date('2017-01-01', 'YYYY-MM-DD')) AS cal_result; cal_result --------------------- 2017-01-31 00:00:00 (1 row) 该函数在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时,使用last_day函数将会调用内置函数b_db_last_day,入参类型可为TEXT、DATE、DATETIME和TIME数据类型。返回值为date类型,支持输入datetime样式数字。
  • sysdate([precision]) 描述:返回函数执行时刻的系统日期和时间。 参数:表示时间精度,int类型,范围为[0,6],缺省时默认值为0。 返回值类型:timestamp without time zone 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb_m=# SELECT sysdate(); sysdate() --------------------- 2023-08-21 17:17:42 (1 row) gaussdb_m=# SELECT sysdate(3); sysdate(3) ------------------------- 2023-08-21 17:17:48.819 (1 row) 该函数在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时有效。
  • transaction_timestamp() 描述:当前事务开始的系统日期及时间。 返回值类型:timestamp with time zone,在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时返回timestamp without time zone。 示例: 1 2 3 4 5 6 7 8 9 10 11 gaussdb=# SELECT transaction_timestamp(); transaction_timestamp ------------------------------- 2017-09-01 17:05:13.534454+08 (1 row) -- MYSQL模式打开兼容性开关 gaussdb=# SELECT transaction_timestamp(); transaction_timestamp ---------------------------- 2023-09-07 09:32:09.728998 (1 row)
  • new_time(date, timezone1,timezone2) 描述:当timezone1所表示时区的日期时间为date时,返回此时timezone2所表示时区的日期时间值。 返回值类型:timestamp 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb=# SELECT new_time('1997-10-10','AST','EST'); new_time --------------------- 1997-10-09 23:00:00 (1 row) gaussdb=# SELECT NEW_TIME(TO_TIMESTAMP ('10-Sep-02 14:10:10.123000','DD-Mon-RR HH24:MI:SS.FF'), 'AST', 'PST'); new_time ------------------------- 2002-09-10 10:10:10.123 (1 row) 此函数在ORA兼容模式数据库中且参数a_format_version值为10c和a_format_dev_version值为s2的情况下生效。
  • sessiontimezone() 描述:当前会话的时区,无入参。 返回值类型:text 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb=# SELECT SESSIONTIMEZONE; session_time_zone ------------------- PST8PDT (1 row) gaussdb=# SELECT LOWER(SESSIONTIMEZONE); lower ----------- @ 8 hours (1 row) 此函数在ORA兼容模式数据库中且参数a_format_version值为10c和a_format_dev_version值为s2的情况下生效。 当set session time zone的值为GMT+08:00/GMT-08:00格式时,正值的偏移量被用于格林威治以西的位置,例如GMT+08:00表示西八区,GMT-08:00表示东八区。
  • sys_extract_utc(timestamp| timestamptz) 描述:从具有时区偏移量或时区区 域名 称的日期时间值中提取UTC(协调世界时-以前称为格林威治平均时间)。如果未指定时区,则日期时间与会话时区关联。入参有timestamp和timestamptz两种形式。 返回值类型:timestamp 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb=# SELECT SYS_EXTRACT_UTC(TIMESTAMP '2000-03-28 11:30:00.00'); sys_extract_utc --------------------- 2000-03-28 03:30:00 (1 row) gaussdb=# SELECT SYS_EXTRACT_UTC(TIMESTAMPTZ '2000-03-28 11:30:00.00 -08:00'); sys_extract_utc --------------------- 2000-03-28 19:30:00 (1 row) 此函数在ORA兼容模式数据库中且参数a_format_version值为10c和a_format_dev_version值为s2的情况下生效。
  • numtoyminterval(num, interval_unit) 描述:将数字转换为interval类型。num为numeric类型数字,interval_unit为固定格式字符串('YEAR' | 'MONTH')。 可以通过设置GUC参数intervalstyle为oracle,兼容该函数interval输出格式。 返回值类型:interval 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 gaussdb=# \c gaussdb_o; Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "gaussdb_o" as user "omm". gaussdb_o=# SET a_format_version='10c'; SET gaussdb_o=# SET a_format_dev_version='s2'; SET gaussdb=# SELECT numtoyminterval(100, 'MONTH'); numtoyminterval ----------------- 8 years 4 mons (1 row) gaussdb_o=# SET intervalstyle = oracle; SET gaussdb_o=# SELECT numtoyminterval(100, 'MONTH'); numtoyminterval ----------------- 8-4 (1 row) gaussdb_o=# \c postgres Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "postgres" as user "omm". gaussdb=# DROP DATEBASE gaussdb_o; DROP DATABASE 此函数在ORA兼容模式数据库中且参数a_format_version值为10c和a_format_dev_version值为s2的情况下有效。
  • tz_offset('time_zone_name' | '(+/-)hh:mi' | SESSIONTIMEZONE | DBTIMEZONE) 描述:入参有以上四种形式,返回入参所表示时区的UTC偏移量。 返回值类型:text 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb=# SELECT TZ_OFFSET('US/Pacific'); tz_offset ----------- -08:00 (1 row) gaussdb=# SELECT TZ_OFFSET(sessiontimezone); tz_offset ----------- +08:00 (1 row) 此函数在ORA兼容模式数据库中且参数a_format_version值为10c和a_format_dev_version值为s2的情况下生效。
  • localtimestamp[([precision])] 描述:当前日期及时间。 参数:表示精度(秒后小数位数),int类型,取值范围为[0,6],缺省时默认值为0,如果超过6,则发出告警,并输出精度为6时的时间值;其他无效值将导致报错。 返回值类型:timestamp without time zone 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 -- 带括号且无入参调用仅在MYSQL兼容模式下支持 gaussdb_m=# SELECT localtimestamp(); timestamp --------------------- 2023-08-21 15:27:59 (1 row) gaussdb_m=# SELECT localtimestamp(3); timestamp ------------------------- 2023-08-21 15:28:02.445 (1 row) -- ORA兼容模式下,打开GUC参数a_format_date_timestamp时 gaussdb=# SET a_format_date_timestamp=on; SET gaussdb=# SELECT localtimestamp(6); localtimestamp ---------------------------- 2023-11-24 11:41:14.086227 (1 row) -- 在ORA兼容模式数据库中且参数a_format_version值为10c、a_format_dev_version值为s2的情况下,precision参数支持numeric类型的整值 gaussdb=# SET a_format_version='10c'; SET gaussdb=# SET a_format_dev_version='s2'; SET gaussdb=# SELECT localtimestamp(6.0); localtimestamp ---------------------------- 2023-11-24 11:42:45.642167 (1 row) 微秒末位的0不显示,如2017-09-01 10:32:19.212000输出显示为2017-09-01 10:32:19.212。 该函数在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时,返回当前SQL语句执行开始时刻的系统日期和时间,支持带括号且无入参调用。 该函数在sql_compatibility = 'ORA',且GUC参数a_format_date_timestamp值为on的情况下,有以下行为: 返回当前SQL语句启动时的系统时间的时间戳。 返回值类型为timestamp without time zone,列名为localtimestamp。 当参数a_format_version值为10c、a_format_dev_version值为s2时,precision参数支持numeric类型的整值,否则仅支持int输入。 关闭GUC参数a_format_date_timestamp,返回的结果为事务开启的系统的日期和时间。
  • now() 描述:当前事务的开始时刻的系统的日期及时间,同一个事务内返回结果相同。仅在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时返回当前SQL语句执行开始时刻的系统日期和时间。 返回值类型:timestamp with time zone,在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时返回类型为timestamp without time zone。 示例: 1 2 3 4 5 6 7 8 9 10 11 gaussdb=# SELECT now(); now ------------------------------- 2017-09-01 17:03:42.549426+08 (1 row) -- MYSQL模式下,打开兼容性参数 gaussdb_m=# SELECT now(); timestamp --------------------- 2023-08-21 17:17:42 (1 row)
  • now(precision) 描述:返回当前SQL语句执行开始时刻的系统日期和时间。 参数:precision表示精度(秒后小数位数),int类型,取值范围为[0,6],缺省时默认值为0,如果超过6,则发出整型值告警,输出精度为6时的时间值;其他无效值将都导致报错。 返回值类型:timestamp without time zone 实现方式:'now' :: text :: timestamp without time zone表达式获取。 示例: 1 2 3 4 5 gaussdb_m=# SELECT now(3); timestamp ------------------------- 2023-08-21 17:17:48.819 (1 row) 该函数在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时有效。
  • dbtimezone 描述:当前数据库的时区。 返回值类型:text 示例: 1 2 3 4 5 gaussdb=# SELECT dbtimezone; dbtimezone ------------------------ PRC (1 row) 此函数在ORA兼容模式数据库中且参数a_format_version值为10c和a_format_dev_version值为s2的情况下有效。
  • numtodsinterval(num, interval_unit) 描述:将数字转换为interval类型。num为numeric类型数字,interval_unit为固定格式字符串('DAY' | 'HOUR' | 'MINUTE' | 'SECOND')。 可以通过设置GUC参数intervalstyle为oracle,兼容该函数interval输出格式。 返回值类型:interval 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 gaussdb=# SELECT numtodsinterval(100, 'HOUR'); numtodsinterval ----------------- 100:00:00 (1 row) gaussdb=# SET intervalstyle = oracle; SET gaussdb=# SELECT numtodsinterval(100, 'HOUR'); numtodsinterval ------------------------------- +000000004 04:00:00.000000000 (1 row) 此函数在ORA兼容模式数据库中且参数a_format_version值为10c和a_format_dev_version值为s2的情况下:当参数interval_unit为 'DAY' 时,参数num超过1000000000会报错。
  • justify_interval(interval) 描述:结合justify_days和justify_hours,调整interval。 返回值类型:interval 示例: 1 2 3 4 5 gaussdb=# SELECT JUSTIFY_INTERVAL(INTERVAL '1 MON -1 HOUR'); justify_interval ------------------ 29 days 23:00:00 (1 row)
  • localtime[([precision])] 描述:返回当前SQL语句执行开始时刻的系统日期和时间。 参数:表示精度(秒后小数位数),int类型,范围为[0,6],缺省时默认值为0,无效值都报错。 返回值类型:timestamp without time zone 实现方式:注册系统函数的localtime。 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb_m=# SELECT localtime(); localtime --------------------- 2023-08-21 15:23:49 (1 row) gaussdb_m=# SELECT localtime(3); localtime ------------------------- 2023-08-21 15:23:51.965 (1 row) 该函数在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时有效。
  • isfinite(date) 描述:判断日期是否为有限值,是则返回t,否则返回f。 返回值类型:Boolean 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb=# SELECT isfinite(date '2001-02-16'); isfinite ---------- t (1 row) gaussdb=# SELECT isfinite(date 'infinity'); isfinite ---------- f (1 row)
  • isfinite(timestamp) 描述:判断时间戳是否为有限值,是则返回t,否则返回f。 返回值类型:Boolean 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb=# SELECT isfinite(timestamp '2001-02-16 21:28:30'); isfinite ---------- t (1 row) gaussdb=# SELECT isfinite(timestamp 'infinity'); isfinite ---------- f (1 row)
  • localtime() 描述:当前事务的开始时刻的系统时间。在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时返回当前SQL语句执行开始时刻的系统日期和时间。 返回值类型:time,在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时返回类型timestamp without time zone。 示例: 1 2 3 4 5 6 7 8 9 10 11 gaussdb=# SELECT localtime AS RESULT; result ---------------- 16:05:55.664681 (1 row) -- MYSQL模式,打开兼容性参数后 gaussdb_m=# SELECT localtime; localtime --------------------- 2023-08-21 15:21:57 (1 row)
  • localtimestamp 描述:当前日期及时间。 返回值类型:timestamp 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 gaussdb=# SELECT localtimestamp; timestamp ---------------------------- 2017-09-01 17:03:30.781902 (1 row) -- MYSQL兼容模式下,打开兼容性参数 gaussdb_m=# SELECT localtimestamp; timestamp --------------------- 2023-08-21 15:27:55 (1 row) -- ORA兼容模式下打开GUC参数a_format_date_timestamp时 gaussdb=# SET a_format_date_timestamp=on; SET gaussdb=# SELECT localtimestamp; localtimestamp ---------------------------- 2023-11-24 11:38:25.633231 (1 row) 该函数在sql_compatibility = 'MYSQL',且参数b_format_version值为5.7、b_format_dev_version值为s1时有如下行为: 返回当前SQL语句执行开始时刻的系统日期和时间。 返回类型timestamp without time zone,列名为timestamp。 该函数在sql_compatibility = 'ORA',且GUC参数a_format_date_timestamp值为on的情况下有如下行为: 返回值类型为timestamp without time zone,列名为localtimestamp。 返回的结果为当前SQL语句启动时的系统时间的时间戳。 防止优化器提前获取常数时间,造成gplan场景下时间获取不正确。 关闭GUC参数a_format_date_timestamp,返回的结果为事务开启的系统的日期和时间。
  • trunc(timestamp) 描述:默认按天截取。 参数:类型为timestamp with time zone、timestamp without time zone。 返回值类型: timestamp without time zone 示例: 1 2 3 4 5 6 7 8 9 10 gaussdb=# SELECT trunc(TIMESTAMP '2001-02-16 20:38:40'); trunc --------------------- 2001-02-16 00:00:00 (1 row) gaussdb=# SELECT trunc(TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40'); trunc --------------------- 2001-02-16 00:00:00 (1 row)
  • date_trunc(text, timestamp) 描述:截取到参数text指定的精度。 返回值类型:interval、timestamp with time zone以及timestamp without time zone。 示例: 1 2 3 4 5 gaussdb=# SELECT date_trunc('hour', timestamp '2001-02-16 20:38:40'); date_trunc --------------------- 2001-02-16 20:00:00 (1 row)
  • daterange(arg1, arg2) 描述:获取时间边界信息。 参数:arg1和arg2的类型为date。 返回值类型:daterange 示例: 1 2 3 4 5 gaussdb=# SELECT daterange('2000-05-06','2000-08-08'); daterange ------------------------- [2000-05-06,2000-08-08) (1 row)
  • trunc(arg1, arg2) 描述:截取到arg2指定的精度。 参数: arg1类型:interval、timestamp with time zone以及timestamp without time zone。 arg2类型:text。arg2支持的单位列表如表1 arg2支持的单位列表所示。 表1 arg2支持的单位列表 单位 含义 mil、mils、millennia、millennium 千禧年 c、cc、cent、centuries、century 世纪 dec、decade、decades、decs 年代 y、year、years、yr、yrs、yyyy 年 q、qtr、quarter 季度 mm、mon、mons、month、months 月 w、week、weeks 周 j、d、day、days、dd、ddd 天 h、hh、hour、hours、hr、hrs 小时 m、mi、min、mins、minute、minutes 分钟 s、sec、second、seconds、secs 秒 millisecon、ms、msec、msecond、mseconds、msecs 毫秒 microsecon、us、usec、usecond、useconds、usecs 微秒 返回值类型:interval、timestamp with time zone以及timestamp without time zone。 示例: 1 2 3 4 gaussdb=# SELECT trunc(timestamp '2001-02-16 20:38:40', 'hour'); trunc --------------------- 2001-02-16 20:00:00 (1 row)
  • round(arg1, arg2) 描述:四舍五入到arg2指定的精度。 参数: arg1类型:timestamp without time zone arg2类型:text 返回值类型:timestamp without time zone 示例: 1 2 3 4 gaussdb=# SELECT round(timestamp '2001-02-16 20:38:40', 'hour'); round --------------------- 2001-02-16 21:00:00 (1 row) 此函数在ORA兼容模式数据库中且参数a_format_version值为10c和a_format_dev_version值为s1的情况下有效。
  • daterange(arg1, arg2, text) 描述:获取时间边界信息。 参数:arg1和arg2的类型为date,text类型为text。 返回值类型:daterange 示例: 1 2 3 4 5 gaussdb=# SELECT daterange('2000-05-06','2000-08-08','[]'); daterange ------------------------- [2000-05-06,2000-08-09) (1 row)
  • pg_systimestamp() 描述:当前日期和时间(当前语句的开始)。 返回值类型:timestamp with time zone 示例: 1 2 3 4 5 gaussdb=# SELECT pg_systimestamp(); pg_systimestamp ------------------------------- 2015-10-14 11:21:28.317367+08 (1 row)
  • systimestamp() 描述:获取当前系统的日期和时间。 返回值类型:timestamp with time zone 示例: 1 2 3 4 5 6 7 8 9 10 11 12 gaussdb=# SELECT systimestamp(); systimestamp ------------------------------- 2024-04-16 15:55:21.614342+08 (1 row) -- behavior_compat_options参数设置sys_function_without_brackets后,支持不带括号调用 gaussdb=# SET behavior_compat_options='sys_function_without_brackets'; gaussdb=# SELECT systimestamp; systimestamp ------------------------------- 2024-04-16 15:55:21.614342+08 (1 row) 该函数仅在sql_compatibility = 'ORA'时生效。
  • systimestamp(precision) 描述:获取当前系统日期及时间,并将结果的微秒部分按照指定precision进行显示。 参数:precision表示精度(秒后小数位数)。INT类型,取值范围[0,6],默认值为6,当超过6时提示WARNING,输出精度为6的时间值。 返回值类型:timestamp with time zone 示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 gaussdb=# SELECT systimestamp(6); systimestamp ------------------------------- 2024-04-16 15:55:21.614342+08 (1 row) gaussdb=# SELECT systimestamp(9); WARNING: TIMESTAMP(9) WITH TIME ZONE precision reduced to maximum allowed, 6 CONTEXT: referenced column: systimestamp systimestamp ------------------------------- 2024-04-16 15:59:49.492505+08 (1 row) gaussdb=# SELECT systimestamp(5.0); systimestamp ------------------------------ 2024-04-16 16:00:23.48079+08 (1 row) 该函数仅在sql_compatibility = 'ORA'时生效。
  • date_part(text, timestamp) 描述:获取日期或者时间值中子域的值,例如年或者小时的值。等效于extract(field from timestamp)。 参数:timestamp类型为abstime、date、interval、reltime、time with time zone、time without time zone、timestamp with time zone以及timestamp without time zone。 返回值类型:double precision 示例: 1 2 3 4 5 gaussdb=# SELECT date_part('hour', timestamp '2001-02-16 20:38:40'); date_part ----------- 20 (1 row)
共100000条
提示

您即将访问非华为云网站,请注意账号财产安全