云数据库 GAUSSDB-时间和日期处理函数和操作符:时间/日期函数
时间/日期函数
- age(timestamp, timestamp)
描述:将两个参数相减,并以年、月、日作为返回值。若相减值为负,则函数返回亦为负,入参可以都带timezone或都不带timezone。
返回值类型:interval
示例:
1 2 3 4 5
openGauss=# SELECT age(timestamp '2001-04-10', timestamp '1957-06-13'); age ------------------------- 43 years 9 mons 27 days (1 row)
- age(timestamp)
描述:当前时间和参数相减,入参可以带或者不带timezone。
返回值类型:interval
示例:
1 2 3 4 5
openGauss=# SELECT age(timestamp '1957-06-13'); age ------------------------- 60 years 2 mons 18 days (1 row)
- clock_timestamp()
描述:实时时钟的当前时间戳。volatile函数,每次扫描都会取最新的时间戳,因此在一次查询中每次调用结果不相同。
返回值类型:timestamp with time zone
示例:
1 2 3 4 5
openGauss=# SELECT clock_timestamp(); clock_timestamp ------------------------------- 2017-09-01 16:57:36.636205+08 (1 row)
- current_date
返回值类型:date
示例:
1 2 3 4 5
openGauss=# SELECT current_date; date ------------ 2017-09-01 (1 row)
- current_time
返回值类型:time with time zone
示例:
1 2 3 4 5
openGauss=# SELECT current_time; timetz -------------------- 16:58:07.086215+08 (1 row)
- current_timestamp
描述:当前日期及时间。语句级别时间,同一个语句内返回结果不变。
返回值类型:timestamp with time zone
示例:
1 2 3 4 5
openGauss=# SELECT current_timestamp; pg_systimestamp ------------------------------ 2017-09-01 16:58:19.22173+08 (1 row)
- pg_systimestamp()
返回值类型:timestamp with time zone
示例:
1 2 3 4 5
openGauss=# SELECT pg_systimestamp(); pg_systimestamp ------------------------------- 2015-10-14 11:21:28.317367+08 (1 row)
- 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
openGauss=# SELECT date_part('hour', timestamp '2001-02-16 20:38:40'); date_part ----------- 20 (1 row)
- timestamp_diff(text, timestamp, timestamp)
描述:计算两个日期时间之间的差值,截取到参数text指定的精度。在兼容MY数据库模式下,该函数功能与TIMESTAMPDIFF(unit , timestamp_expr1, timestamp_expr2)相同。
返回值类型:int64
示例:
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
openGauss=# SELECT timestamp_diff('year','2018-01-01','2020-04-01'); timestamp_diff ---------------- 2 (1 row) openGauss=# SELECT timestamp_diff('month','2018-01-01','2020-04-01'); timestamp_diff ---------------- 27 (1 row) openGauss=# SELECT timestamp_diff('quarter','2018-01-01','2020-04-01'); timestamp_diff ---------------- 9 (1 row) openGauss=# SELECT timestamp_diff('week','2018-01-01','2020-04-01'); timestamp_diff ---------------- 117 (1 row) openGauss=# SELECT timestamp_diff('day','2018-01-01','2020-04-01'); timestamp_diff ---------------- 821 (1 row) openGauss=# SELECT timestamp_diff('hour','2018-01-01 10:10:10','2018-01-01 12:12:12'); timestamp_diff ---------------- 2 (1 row) openGauss=# SELECT timestamp_diff('minute','2018-01-01 10:10:10','2018-01-01 12:12:12'); timestamp_diff ---------------- 122 (1 row) openGauss=# SELECT timestamp_diff('second','2018-01-01 10:10:10','2018-01-01 10:12:12'); timestamp_diff ---------------- 122 (1 row) openGauss=# SELECT timestamp_diff('microsecond','2018-01-01 10:10:10','2018-01-01 10:12:12'); timestamp_diff ---------------- 122000000 (1 row)
- date_part(text, interval)
描述:获取月份的值。如果大于12,则取与12的模。等效于extract(field from timestamp)。
返回值类型:double precision
示例:
1 2 3 4 5
openGauss=# SELECT date_part('month', interval '2 years 3 months'); date_part ----------- 3 (1 row)
- date_trunc(text, timestamp)
返回值类型:interval、timestamp with time zone、timestamp without time zone
示例:
1 2 3 4 5
openGauss=# SELECT date_trunc('hour', timestamp '2001-02-16 20:38:40'); date_trunc --------------------- 2001-02-16 20:00:00 (1 row)
- trunc(timestamp)
示例:
1 2 3 4
openGauss=# SELECT trunc(timestamp '2001-02-16 20:38:40'); trunc --------------------- 2001-02-16 00:00:00 (1 row)
- trunc(arg1, arg2)
arg1类型:interval、timestamp with time zone、timestamp without time zone
arg2类型:text
返回值类型:interval、timestamp with time zone、timestamp without time zone
示例:
1 2 3 4
openGauss=# SELECT trunc(timestamp '2001-02-16 20:38:40', 'hour'); trunc --------------------- 2001-02-16 20:00:00 (1 row)
- daterange(arg1, arg2)
arg1类型:date
arg2类型:date
返回值类型:daterange
示例:
1 2 3 4 5
openGauss=# select daterange('2000-05-06','2000-08-08'); daterange ------------------------- [2000-05-06,2000-08-08) (1 row)
- daterange(arg1, arg2, text)
arg1类型:date
arg2类型:date
text类型:text
返回值类型:daterange
示例:
1 2 3 4 5
openGauss=# select daterange('2000-05-06','2000-08-08','[]'); daterange ------------------------- [2000-05-06,2000-08-09) (1 row)
- extract(field from timestamp)
返回值类型:double precision
示例:
1 2 3 4 5
openGauss=# SELECT extract(hour from timestamp '2001-02-16 20:38:40'); date_part ----------- 20 (1 row)
- extract(field from interval)
返回值类型:double precision
示例:
1 2 3 4 5
openGauss=# SELECT extract(month from interval '2 years 3 months'); date_part ----------- 3 (1 row)
- isfinite(date)
返回值类型:Boolean
示例:
1 2 3 4 5
openGauss=# SELECT isfinite(date '2001-02-16'); isfinite ---------- t (1 row)
- isfinite(timestamp)
返回值类型:Boolean
示例:
1 2 3 4 5
openGauss=# SELECT isfinite(timestamp '2001-02-16 21:28:30'); isfinite ---------- t (1 row)
- isfinite(interval)
返回值类型:Boolean
示例:
1 2 3 4 5
openGauss=# SELECT isfinite(interval '4 hours'); isfinite ---------- t (1 row)
- justify_days(interval)
返回值类型:interval
示例:
1 2 3 4 5
openGauss=# SELECT justify_days(interval '35 days'); justify_days -------------- 1 mon 5 days (1 row)
- justify_hours(interval)
返回值类型:interval
示例:
1 2 3 4 5
openGauss=# SELECT JUSTIFY_HOURS(INTERVAL '27 HOURS'); justify_hours ---------------- 1 day 03:00:00 (1 row)
- justify_interval(interval)
描述:结合justify_days和justify_hours,调整interval。
返回值类型:interval
示例:
1 2 3 4 5
openGauss=# SELECT JUSTIFY_INTERVAL(INTERVAL '1 MON -1 HOUR'); justify_interval ------------------ 29 days 23:00:00 (1 row)
- localtime
返回值类型:time
示例:
1 2 3 4 5
openGauss=# SELECT localtime AS RESULT; result ---------------- 16:05:55.664681 (1 row)
- localtimestamp
返回值类型:timestamp
示例:
1 2 3 4 5
openGauss=# SELECT localtimestamp; timestamp ---------------------------- 2017-09-01 17:03:30.781902 (1 row)
- now()
描述:当前日期及时间。事务级别时间,同一个事务内返回结果相同。
返回值类型:timestamp with time zone
示例:
1 2 3 4 5
openGauss=# SELECT now(); now ------------------------------- 2017-09-01 17:03:42.549426+08 (1 row)
- timenow()
返回值类型:timestamp with time zone
示例:
1 2 3 4 5
openGauss=# select timenow(); timenow ------------------------ 2020-06-23 20:36:56+08 (1 row)
- numtodsinterval(num, interval_unit)
描述:将数字转换为interval类型。num为numeric类型数字,interval_unit为固定格式字符串('DAY' | 'HOUR' | 'MINUTE' | 'SECOND')。
可以通过设置GUC参数IntervalStyle为oracle,兼容该函数在Oracle中的interval输出格式。
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13
openGauss=# SELECT numtodsinterval(100, 'HOUR'); numtodsinterval ----------------- 100:00:00 (1 row) openGauss=# SET intervalstyle = oracle; SET openGauss=# SELECT numtodsinterval(100, 'HOUR'); numtodsinterval ------------------------------- +000000004 04:00:00.000000000 (1 row)
- pg_sleep(seconds)
描述:服务器线程延迟时间,单位为秒。注意,当数据库调用该函数时,会获取相应的事务快照,相当于一个长事务,如果入参时间过长可能导致数据库oldestxmin无法推进,影响表的回收和查询性能。
返回值类型:void
示例:
1 2 3 4 5
openGauss=# SELECT pg_sleep(10); pg_sleep ---------- (1 row)
- statement_timestamp()
返回值类型:timestamp with time zone
示例:
1 2 3 4 5
openGauss=# SELECT statement_timestamp(); statement_timestamp ------------------------------- 2017-09-01 17:04:39.119267+08 (1 row)
- sysdate
返回值类型:timestamp
示例:
1 2 3 4 5
openGauss=# SELECT sysdate; sysdate --------------------- 2017-09-01 17:04:49 (1 row)
- timeofday()
描述:当前日期及时间(像clock_timestamp,但是返回时为text)。
返回值类型:text
示例:
1 2 3 4 5
openGauss=# SELECT timeofday(); timeofday ------------------------------------- Fri Sep 01 17:05:01.167506 2017 CS T (1 row)
- transaction_timestamp()
描述:当前日期及时间,与current_timestamp等效。
返回值类型:timestamp with time zone
示例:
1 2 3 4 5
openGauss=# SELECT transaction_timestamp(); transaction_timestamp ------------------------------- 2017-09-01 17:05:13.534454+08 (1 row)
- add_months(d,n)
d:timestamp类型的值,以及可以隐式转换为timestamp类型的值。
n:INTEGER类型的值,以及可以隐式转换为INTEGER类型的值。
返回值类型:timestamp
示例:
1 2 3 4 5
openGauss=# SELECT add_months(to_date('2017-5-29', 'yyyy-mm-dd'), 11) FROM sys_dummy; add_months --------------------- 2018-04-29 00:00:00 (1 row)
- last_day(d)
返回值类型:timestamp
示例:
1 2 3 4 5
openGauss=# select last_day(to_date('2017-01-01', 'YYYY-MM-DD')) AS cal_result; cal_result --------------------- 2017-01-31 00:00:00 (1 row)
- 华为数据库日期函数_GaussDB安装_高斯数据库日期函数
- GaussDB数据库函数_GaussDB函数和操作符_高斯数据库函数-华为云
- GaussDB数据库函数和字符串_高斯数据库优势_新建高斯数据库_高斯协议
- GaussDB获取时间_gaussdb for MySQL_高斯数据库获取时间_华为云
- GaussDB求日期差_GaussDB数据类型_高斯数据库求日期差-华为云
- GaussDB设置默认值_GaussDB存储过程_高斯数据库设置默认值-华为云
- 域名备案需要多少时间
- 数据转发至函数工作流_数据处理_FunctionGraph处理流数据
- GaussDB培训_GaussDB教程_高斯数据库培训-华为云
- GaussDB函数_GaussDB数据库函数_高斯数据库函数_华为云