云数据库 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()
返回值类型: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)
- 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)
- 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和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和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')。
可以通过设置参数IntervalStyle为a,兼容该函数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 = a; SET openGauss=# SELECT numtodsinterval(100, 'HOUR'); numtodsinterval ------------------------------- +000000004 04:00:00.000000000 (1 row)
- pg_sleep(seconds)
返回值类型: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数据库函数_高斯数据库函数_华为云