云数据库 GaussDB-时间和日期处理函数和操作符:时间/日期函数
时间/日期函数
- age(timestamp, timestamp)
描述:将两个参数相减,并以年、月、日作为返回值。若相减值为负,则函数返回亦为负,入参可以都带timezone或都不带timezone。
返回值类型:interval
示例:
12345
gaussdb=# SELECT age(timestamp '2001-04-10', timestamp '1957-06-13'); age ------------------------- 43 years 9 mons 27 days(1 row)
- age(timestamp)
描述:当前时间和参数相减,入参可以带或者不带timezone。
返回值类型:interval
示例:
12345
gaussdb=# SELECT age(timestamp '1957-06-13'); age ------------------------- 60 years 2 mons 18 days(1 row)
- clock_timestamp()
返回值类型:timestamp with time zone
示例:
12345
gaussdb=# SELECT clock_timestamp(); clock_timestamp ------------------------------- 2017-09-01 16:57:36.636205+08(1 row)
- current_date
返回值类型:date
示例:
12345
gaussdb=# SELECT current_date; date ------------ 2017-09-01(1 row)
此函数在参数a_format_version值为10c和a_format_dev_version值为s2的情况下返回值类型为timestamp。
- current_time
返回值类型:time with time zone
示例:
12345
gaussdb=# SELECT current_time; timetz -------------------- 16:58:07.086215+08(1 row)
- current_timestamp
返回值类型:timestamp with time zone
示例:
12345
gaussdb=# SELECT current_timestamp; pg_systimestamp ------------------------------ 2017-09-01 16:58:19.22173+08(1 row)
- current_timestamp(precision)
返回值类型:timestamp with time zone
示例:
12345
gaussdb=# SELECT current_timestamp(1); timestamptz ------------------------------ 2017-09-01 16:58:19.2+08(1 row)
- 此函数在参数a_format_version值为10c和a_format_dev_version值为s2的情况下,precision参数支持numeric类型的整值,否则仅支持int输入。
- 微秒末位的0不显示。如 2017-09-01 10:32:19.212000 输出显示为 2017-09-01 10:32:19.212。
- 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
示例:
12345
gaussdb=# 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
示例:
12345
gaussdb=# SELECT date_part('month', interval '2 years 3 months'); date_part ----------- 3(1 row)
- timestamp_diff(text, timestamp, timestamp)
描述:计算两个日期时间之间的差值,截取到参数text指定的精度。
返回值类型:int64
示例:
1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839404142434445
gaussdb=# SELECT timestamp_diff('year','2018-01-01','2020-04-01'); timestamp_diff ---------------- 2(1 row)gaussdb=# SELECT timestamp_diff('month','2018-01-01','2020-04-01'); timestamp_diff ---------------- 27(1 row)gaussdb=# SELECT timestamp_diff('quarter','2018-01-01','2020-04-01'); timestamp_diff ---------------- 9(1 row)gaussdb=# SELECT timestamp_diff('week','2018-01-01','2020-04-01'); timestamp_diff ---------------- 117(1 row)gaussdb=# SELECT timestamp_diff('day','2018-01-01','2020-04-01'); timestamp_diff ---------------- 821(1 row)gaussdb=# SELECT timestamp_diff('hour','2018-01-01 10:10:10','2018-01-01 12:12:12'); timestamp_diff ---------------- 2(1 row)gaussdb=# SELECT timestamp_diff('minute','2018-01-01 10:10:10','2018-01-01 12:12:12'); timestamp_diff ---------------- 122(1 row)gaussdb=# SELECT timestamp_diff('second','2018-01-01 10:10:10','2018-01-01 10:12:12'); timestamp_diff ---------------- 122(1 row)gaussdb=# SELECT timestamp_diff('microsecond','2018-01-01 10:10:10','2018-01-01 10:12:12'); timestamp_diff ---------------- 122000000(1 row)
- date_trunc(text, timestamp)
返回值类型:interval、timestamp with time zone、timestamp without time zone
示例:
12345
gaussdb=# SELECT date_trunc('hour', timestamp '2001-02-16 20:38:40'); date_trunc --------------------- 2001-02-16 20:00:00(1 row)
- trunc(timestamp)
示例:
1234
gaussdb=# 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
示例:
1234
gaussdb=# SELECT trunc(timestamp '2001-02-16 20:38:40', 'hour'); trunc---------------------2001-02-16 20:00:00(1 row)
- round(arg1, arg2)
arg1类型:timestamp without time zone
arg2类型:text
返回值类型:timestamp without time zone
示例:
1234
gaussdb=# SELECT round(timestamp '2001-02-16 20:38:40', 'hour'); round---------------------2001-02-16 21:00:00(1 row)
此函数在参数a_format_version值为10c和a_format_dev_version值为s1的情况下有效。
- daterange(arg1, arg2)
描述:获取时间边界信息。arg1和arg2的类型为date。
返回值类型:daterange
示例:
12345
gaussdb=# 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
示例:
12345
gaussdb=# select daterange('2000-05-06','2000-08-08','[]'); daterange ------------------------- [2000-05-06,2000-08-09)(1 row)
- extract(field from timestamp)
返回值类型:double precision
示例:
12345
gaussdb=# SELECT extract(hour from timestamp '2001-02-16 20:38:40'); date_part ----------- 20(1 row)
- extract(field from interval)
返回值类型:double precision
示例:
12345
gaussdb=# SELECT extract(month from interval '2 years 3 months'); date_part ----------- 3(1 row)
- isfinite(date)
返回值类型:Boolean
示例:
12345
gaussdb=# SELECT isfinite(date '2001-02-16'); isfinite ---------- t(1 row)
- isfinite(timestamp)
返回值类型:Boolean
示例:
12345
gaussdb=# SELECT isfinite(timestamp '2001-02-16 21:28:30'); isfinite ---------- t(1 row)
- isfinite(interval)
返回值类型:Boolean
示例:
12345
gaussdb=# SELECT isfinite(interval '4 hours'); isfinite ---------- t(1 row)
- justify_days(interval)
返回值类型:interval
示例:
12345
gaussdb=# SELECT justify_days(interval '35 days'); justify_days -------------- 1 mon 5 days(1 row)
- justify_hours(interval)
返回值类型:interval
示例:
12345
gaussdb=# SELECT JUSTIFY_HOURS(INTERVAL '27 HOURS'); justify_hours ---------------- 1 day 03:00:00(1 row)
- justify_interval(interval)
描述:结合justify_days和justify_hours,调整interval。
返回值类型:interval
示例:
12345
gaussdb=# SELECT JUSTIFY_INTERVAL(INTERVAL '1 MON -1 HOUR'); justify_interval ------------------ 29 days 23:00:00(1 row)
- localtime
返回值类型:time
示例:
12345
gaussdb=# SELECT localtime AS RESULT; result---------------- 16:05:55.664681(1 row)
- localtimestamp
返回值类型:timestamp
示例:
12345
gaussdb=# SELECT localtimestamp; timestamp ---------------------------- 2017-09-01 17:03:30.781902(1 row)
- now()
返回值类型:timestamp with time zone
示例:
12345
gaussdb=# SELECT now(); now ------------------------------- 2017-09-01 17:03:42.549426+08(1 row)
- timenow()
返回值类型:abstime
示例:
12345
gaussdb=# select timenow(); timenow------------------------ 2020-06-23 20:36:56+08(1 row)
- dbtimezone
返回值类型:text
示例:
12345
gaussdb=# select dbtimezone; dbtimezone------------------------ PRC(1 row)
此函数在参数a_format_version值为10c和a_format_dev_version值为s2的情况下有效。
- numtodsinterval(num, interval_unit)
描述:将数字转换为interval类型。num为numeric类型数字,interval_unit为固定格式字符串('DAY' | 'HOUR' | 'MINUTE' | 'SECOND')。
可以通过设置GUC参数IntervalStyle为a,兼容该函数interval输出格式。
返回值类型:interval
示例:
1 2 3 4 5 6 7 8 910111213
gaussdb=# SELECT numtodsinterval(100, 'HOUR'); numtodsinterval ----------------- 100:00:00(1 row)gaussdb=# SET intervalstyle = a;SETgaussdb=# SELECT numtodsinterval(100, 'HOUR'); numtodsinterval------------------------------- +000000004 04:00:00.000000000(1 row)
此函数在参数a_format_version值为10c和a_format_dev_version值为s2的情况下:当参数interval_unit为 'DAY' 时,参数num超过1000000000会报错。
- numtoyminterval(num, interval_unit)
描述:将数字转换为interval类型。num为numeric类型数字,interval_unit为固定格式字符串('YEAR' | 'MONTH')。
可以通过设置GUC参数IntervalStyle为a,兼容该函数interval输出格式。
返回值类型:interval
示例:
1 2 3 4 5 6 7 8 910111213
gaussdb=# SELECT numtoyminterval(100, 'MONTH'); numtoyminterval ----------------- 8 years 4 mons(1 row)gaussdb=# SET intervalstyle = oracle;SETgaussdb=# SELECT numtodsinterval(100, 'MONTH'); numtoyminterval ----------------- 8-4(1 row)
此函数在参数a_format_version值为10c和a_format_dev_version值为s2的情况下有效。
- new_time(date, timezone1,timezone2)
描述:当timezone1所表示时区的日期时间为date的时候,返回此时timezone2所表示时区的日期时间值。
返回值类型:timestamp
示例:
1 2 3 4 5 6 7 8 910
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)
此函数在参数a_format_version值为10c和a_format_dev_version值为s2的情况下生效。
- sessiontimezone
返回值类型:text。
示例:
1 2 3 4 5 6 7 8 910
gaussdb=# SELECT SESSIONTIMEZONE; session_time_zone ------------------- PST8PDT(1 row)gaussdb=# SELECT LOWER(SESSIONTIMEZONE); lower ----------- @ 8 hours(1 row)
此函数在参数a_format_version值为10c和a_format_dev_version值为s2的情况下生效。
- sys_extract_utc(timestamp| timestamptz)
描述:从具有时区偏移量或时区区域名称的日期时间值中提取UTC(协调世界时-以前称为格林威治平均时间)。如果未指定时区,则日期时间与会话时区关联。入参有timestmp和timestamp两种形式。
返回值类型:timestamp。
示例:
1 2 3 4 5 6 7 8 910
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)
此函数在参数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 910
gaussdb=# SELECT TZ_OFFSET('US/Pacific'); tz_offset ----------- -08:00(1 row)gaussdb=# SELECT TZ_OFFSET(sessiontimezone); tz_offset ----------- +08:00(1 row)
此函数在参数a_format_version值为10c和a_format_dev_version值为s2的情况下生效。
- pg_sleep(seconds)
返回值类型:void
示例:
12345
gaussdb=# SELECT pg_sleep(10); pg_sleep ----------(1 row)
- statement_timestamp()
返回值类型:timestamp with time zone
示例:
12345
gaussdb=# SELECT statement_timestamp(); statement_timestamp ------------------------------- 2017-09-01 17:04:39.119267+08(1 row)
- sysdate
返回值类型:timestamp
示例:
12345
gaussdb=# SELECT sysdate; sysdate --------------------- 2017-09-01 17:04:49(1 row)
- timeofday()
描述:当前日期及时间(像clock_timestamp,但是返回时为text)。
返回值类型:text
示例:
12345
gaussdb=# SELECT timeofday(); timeofday ------------------------------------- Fri Sep 01 17:05:01.167506 2017 CS T(1 row)
- transaction_timestamp()
描述:当前日期及时间,与current_timestamp等效。
返回值类型:timestamp with time zone
示例:
12345
gaussdb=# 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
示例:
12345
gaussdb=# 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)
此函数在参数a_format_version值为10c和a_format_dev_version值为s1的情况下:- 当计算结果大于公元9999年时会报错。
- 参数n入参若为小数则不会被四舍五入,而是被截断。
- last_day(d)
返回值类型:timestamp
示例:
12345
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)
- 华为数据库日期函数_GaussDB安装_高斯数据库日期函数
- GaussDB数据库函数和字符串_高斯数据库优势_新建高斯数据库_高斯协议
- GaussDB数据库函数_GaussDB函数和操作符_高斯数据库函数-华为云
- GaussDB获取时间_gaussdb for MySQL_高斯数据库获取时间_华为云
- GaussDB设置默认值_GaussDB存储过程_高斯数据库设置默认值-华为云
- GaussDB求日期差_GaussDB数据类型_高斯数据库求日期差-华为云
- GaussDB函数_GaussDB数据库函数_高斯数据库函数_华为云
- 如何创建定时压测_性能测试服务_定时压测-华为云
- GaussDB培训_GaussDB教程_高斯数据库培训-华为云
- 调用函数_函数调用方式_函数工作流 FunctionGraph-华为云