Oracle日期型函数详解

本文详细介绍了Oracle数据库中关于时间与日期处理的常用函数,包括sysdate、interval、add_months、months_between、round、trunc、extract、new_time、localtimestamp、current_timestamp、current_date、dbtimezone和sessiontimezone等。通过示例展示了如何获取当前时间、进行时间间隔计算、日期截取、时间转换等操作,是Oracle数据库开发者的重要参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.sysdate:返回系统当前时间

函数说明】:该函数返回系统当前时间,没有参数,不带括号。

样例展示】:

select sysdate from dual;        --获取系统当前时间:2022-02-19 10:12:22
select to_char(sysdate,'yyyy-mm-dd HH24:mi:ss') from dual;        --按指定格式获取当前时间:2022-02-19 10:12:22
select to_char(sysdate,'yyyy') from dual;        --获取当前年份:2022
select to_char(sysdate,'mm') from dual;        --获取当前月份:02
select to_char(sysdate,'hh24') from dual;        --获取当前小时(24小时制):10
select to_char(sysdate,'hh') from dual;        --获取当前小时(12小时制):10
select to_char(sysdate,'mi') from dual;        --获取当前分钟:12
select to_char(sysdate,'ss') from dual;        --获取当前秒:22
select sysdate + interval '1' year from dual;        --当前时间加上1年:2023-02-19 10:12:22
select sysdate - interval '1' year from dual;        --当前时间减去1年:2021-02-19 10:12:22
select sysdate + interval '1' month from dual;        --当前时间加上1个月:2022-03-19 10:12:22
select sysdate - interval '1' month from dual;        --当前时间减去1个月:2022-01-19 10:12:22
select sysdate + interval '1' day from dual;        --当前时间加上1天:2022-02-20 10:12:22
select sysdate - interval '1' day from dual;        --当前时间减去1天:2022-02-18 10:12:22
select sysdate + interval '1' hour from dual;        --当前时间加上1小时:2022-02-19 11:12:22
select sysdate - interval '1' hour from dual;        --当前时间减去1小时:2022-02-19 9:12:22
select sysdate + interval '1' minute from dual;        --当前时间加上1分钟:2022-02-19 10:13:22
select sysdate - interval '1' minute from dual;        --当前时间减去1分钟:2022-02-19 10:11:22
select sysdate + interval '1' second from dual;        --当前时间加上1秒钟:2022-02-19 10:12:23
select sysdate - interval '1' second from dual;        --当前时间减去1秒钟:2022-02-19 10:12:21

2.interval:返回一段时间间隔

  • 【用法一:返回一段只精确到年和月的时间间隔】

        【函数格式】:interval 'integer[-integer]' {year|month} [(precision)][to{year|month}]

        【函数说明】:该函数返回一段时间间隔,时间间隔只精确到年和月,precision为年或月的精确域,有效范围是0到9,若省略该参数,则默认为2。

        【注意事项】

        (1).precision为年或月的精确域,有效范围为0至9,若省略该参数,则默认为2。

        (2).当参数precision有值时,若该参数跟在year后面,则不能小于表示年的参数integer的位数,若该参数跟在month后面,则不能小于表示月的参数integer值换算成年数值的位数,否则执行时会报错“间隔的前导精度太小”。

        (3).当省略表示月的参数integer,则也可以省略[to{year|month}]部分,若[to{year|month}]有值,则必须与{year|month}保持一致,即必须是year to yearmonth to month的形式。

        样例展示一】:该函数不同形式的写法以及返回结果

select interval '56' year from dual;        --返回:+56-00(表示56年)
select interval '56' year(1) from dual;        --报错【间隔的前导精度太小】
select interval '567' year from dual;        --报错【间隔的前导精度太小】
select interval '567' year(3) from dual;        --返回:+567-00(表示567年0个月)
select interval '56' month from dual;        --大于等于12则返回年月:+04-08(表示4年8个月)
select interval '56' month(1) from dual;        --返回:+4-08(表示4年8个月)【此处精度1针对56换算成的年数4】
select interval '567' month(1) from dual;        --报错【间隔的前导精度太小】【此处精度1针对567换算成的年数47】
select interval '567' month(2) from dual;        --返回:+47-03【此处精度2针对567换算成的年数47】
select interval '567-5' year(3) to month from dual;        --返回:+567-05(表示567年5个月)
select interval '567-5' year(4) to month from dual;        --返回:+0567-05(表示567年5个月)
select interval '567-5' year to month from dual;        --报错【间隔的前导精度太小】
select interval '56-5' year to month from dual;        --返回:+56-05(表示56年5个月)
select interval '56' year to year from dual;        --返回:+56-00(表示56年)
select interval '56-0' year to month from dual;        --返回:+56-00表示56年)】                select interval '56' month to month from dual;        --返回:+04-08(表示4年8个月)

  样例展示二】:多个时间间隔之间做加法运算

select interval '1' year + interval '2' month from dual;        --返回:+000000001-02
select interval '1' year + interval '20' month from dual;        --返回:+000000002-08
select interval '1' year + interval '1' year from dual;        --返回:+000000002-00
select interval '1' month + interval '1' month from dual;        --返回:+000000000-02
select interval '1-0' year to month + interval '1' month from dual;        --返回:+000000001-01
select interval '1-0' year to month + interval '1-0' year to month from dual;        --返回:+000000002-00
select interval '1-0' year to month + interval '1' year from dual;        --返回:+000000002-00

  样例展示三】:多个时间间隔之间做减法运算

select interval '1' year - interval '2' month from dual;        --返回:+000000000-10
select interval '1' year - interval '20' month from dual;        --返回:-000000000-08
select interval '1' year - interval '1' year from dual;        --返回:+000000000-00
select interval '1' month - interval '1' month from dual;        --返回:+000000000-00
select interval '1-0' year to month - interval '1' month from dual;        --返回:+000000000-11
select interval '2-0' year to month - interval '1-0' year to month from dual;        --返回:+000000001-00
select interval '2-5' year to month - interval '1' year from dual;        --返回:+000000001-05

  • 【用法二:返回一段精确到时分秒的时间间隔】

        【函数格式】:interval '{ integer | integer time_expr | time_expr }'{ { day| hour| minute} [ ( leading_precision ) ]| second[ ( leading_precision [, fractional_seconds_precision ] ) ] }
[ TO { day| hour| minute| second[ (fractional_seconds_precision) ] } ]

        【函数说明】:该函数返回一段精确到时分秒的时间间隔,参数leading_precision值的范围为0至9,若省略该参数,则默认为2。

        【注意事项】:time_expr的格式为:HH[:MI[:SS[.n]]] MI[:SS[.n]] SS[.n]n表示微秒。
hour范围为0至23,minute范围为: 0至59,second范围为: 0至59.999999999。

       样例展示一】:展示以day开头的用法

select interval '1' day from dual;        --返回:+01 00:00:00(表示: 1天)
select interval '1' day(3) from dual;        --返回:+001 00:00:00(表示: 1天)
select interval '1 2' day(1) to hour from dual;        --返回:+1 02:00:00(表示: 1天2小时)
select interval '1 2:10' day(2) to minute from dual;        --返回:+01 02:10:00(表示: 1天2小时10分)
select interval '1 2:10:30.555' day to second from dual;        --返回:+01 02:10:30.555000(表示: 1天2小时10分30.555秒)
select interval '1 2:10:30.555' day to second(5) from dual;        --返回:+01 02:10:30.55500(表示: 1天2小时10分30.555秒)
select interval '1 2:10:30.555' day to second(3) from dual;        --返回:+01 02:10:30.555(表示: 1天2小时10分30.555秒)
select interval '1 2:10:30.555' day(1) to second(3) from dual;        --返回:+1 02:10:30.555(表示: 1天2小时10分30.555秒)
select interval '1 2:10:30.555' day(3) to second(2) from dual;        --返回:+001 02:10:30.56(表示: 1天2小时10分30.56秒)

       样例展示二】:展示以hour开头的用法

select interval '2' hour from dual;        --返回:+00 02:00:00(表示: 2小时)
select interval '2' hour(3) from dual;        --返回:+000 02:00:00(表示: 2小时)
select interval '2:10' hour(3) to minute from dual;        --返回:+000 02:10:00(表示: 2小时10分)
select interval '2:10:30.555' hour(3) to second from dual;        --返回:+000 02:10:30.555000(表示: 2小时10分30.555秒)
select interval '2:10:30.555' hour to second(3) from dual;        --返回:+00 02:10:30.555(表示: 2小时10分30.555秒)
select interval '2:10:30.555' hour to second(1) from dual;        --返回:+00 02:10:30.6(表示: 2小时10分30.6秒)

       样例展示三】:展示以minute开头的用法

select interval '10' minute from dual;        --返回:+00 00:10:00(表示: 10分钟)
select interval '10' minute(3) from dual;        --返回:+000 00:10:00(表示: 10分钟)
select interval '10:30.555' minute(3) to second from dual;        --返回:+000 00:10:30.555000(表示: 10分30.555秒)
select interval '10:30.555' minute to second(3) from dual;        --返回:+00 00:10:30.555(表示: 10分30.555秒)
select interval '10:30.555' minute to second(1) from dual;        --返回:+00 00:10:30.6(表示: 10分30.6秒)

       样例展示四】:展示以second开头的用法

select interval '30.555' second from dual;        --返回:+00 00:00:30.555000
select interval '30.555' second(1) from dual;        --返回:+0 00:00:30.555000
select interval '30.555' second(3) from dual;        --返回:+000 00:00:30.555000

       样例展示五】:展示多个时间间隔的加减法运算

select interval '1' day + interval '1' day from dual;        --返回:+000000002 00:00:00.000000000
select interval '1' day + interval '2' hour from dual;        --返回:+000000001 02:00:00.000000000
select interval '2' hour - interval '2' hour from dual;        --返回:+000000000 00:00:00.000000000
select interval '2' hour - interval '10' minute from dual;        --返回:+000000000 01:50:00.000000000

3.add_months:返回指定日期进行加减运算后的结果

【函数格式】:add_months(dateTime,intNum)

【函数说明】:该函数返回时间参数dateTime加上月份intNum后的结果,intNum可以为负数。

【样例展示】:

select add_months(sysdate,1) from dual;        --返回:2022/3/19 13:25:19
select add_months(sysdate,1.5) from dual;        --返回:2022/3/19 13:25:19
select add_months(sysdate,-1) from dual;        --返回:2022/1/19 13:25:19
select add_months(sysdate,-1.5) from dual;        --返回:2022/1/19 13:25:19

4.months_between:返回两个日期之间的月份数

【函数格式】:months_between(startDate,endDate)

【函数说明】:该函数返回两个日期startDateendDate之间的月份数,当startDate大于endDate时,返回正数,当startDate小于endDate时,返回负数,当startDate等于endDate时,返回0。

【样例展示】:

select months_between(to_date('20220219', 'yyyymmdd'), to_date('20250219', 'yyyymmdd')) from dual;        --返回:-36
select months_between(to_date('20250219', 'yyyymmdd'), to_date('20220219', 'yyyymmdd')) from dual;        --返回:36
select months_between(to_date('20220219', 'yyyymmdd'), to_date('20220219', 'yyyymmdd')) from dual;        --返回:0
select months_between(to_date('20220220', 'yyyymmdd'), to_date('20220219', 'yyyymmdd')) from dual;        --返回:0.032258064516129
select months_between(to_date('20220320', 'yyyymmdd'), to_date('20220219', 'yyyymmdd')) from dual;        --返回:1.03225806451613
select months_between(to_date('20220320', 'yyyymmdd'), to_date('20220819', 'yyyymmdd')) from dual;        --返回:-4.96774193548387

5.last_day:返回指定时间所在月份的最后一天

【函数格式】:last_day(dateTime)

【函数说明】:该函数返回时间参数dateTime所在月份的最后一天。

【样例展示】:

select last_day(sysdate) from dual;        --返回:2022/2/28 14:12:04
select last_day(to_date('2022/2/12','yyyy-mm-dd')) from dual;        --返回:2022/2/28
select last_day(to_date('2022/2/12 14:12:04','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:2022/2/28 14:12:04
select last_day(to_date('2022/2','yyyy-mm')) from dual;        --返回:2022/2/28
select last_day(to_date('2022/3','yyyy-mm')) from dual;        --返回:2022/3/31
select last_day(to_date('2022/4','yyyy-mm')) from dual;        --返回:2022/4/30

6.next_day:返回指定参数的下一个日期

【函数格式】:next_day(dateTime[,dateFormat])

【函数说明】:参数dateFormat小于dateTime对应的星期数,则返回下周的相应日期;若参数dateFormat大于dateTime对应的星期数,则返回本周的相应日期。

【参数说明】:参数dateTime为时间类型,参数dateFormat为字符型,该参数可选。

        参数dateFormat取值范围:星期一至星期日

【样例展示一】:dateTime对应星期三时。

select next_day(to_date('2022/2/16 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期一') from dual;        --返回:2022/2/21 16:07:43
select next_day(to_date('2022/2/16 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期二') from dual;        --返回:2022/2/22 16:07:43
select next_day(to_date('2022/2/16 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期三') from dual;        --返回:2022/2/23 16:07:43
select next_day(to_date('2022/2/16 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期四') from dual;        --返回:2022/2/17 16:07:43
select next_day(to_date('2022/2/16 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期五') from dual;        --返回:2022/2/18 16:07:43
select next_day(to_date('2022/2/16 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期六') from dual;        --返回:2022/2/19 16:07:43
select next_day(to_date('2022/2/16 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期日') from dual;        --返回:2022/2/20 16:07:43

【样例展示二】:dateTime对应星期六时。

select next_day(to_date('2022/2/19 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期一') from dual;        --返回:2022/2/21 16:07:43
select next_day(to_date('2022/2/19 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期二') from dual;        --返回:2022/2/22 16:07:43
select next_day(to_date('2022/2/19 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期三') from dual;        --返回:2022/2/23 16:07:43
select next_day(to_date('2022/2/19 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期四') from dual;        --返回:2022/2/24 16:07:43
select next_day(to_date('2022/2/19 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期五') from dual;        --返回:2022/2/25 16:07:43
select next_day(to_date('2022/2/19 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期六') from dual;        --返回:2022/2/26 16:07:43
select next_day(to_date('2022/2/19 16:07:43','yyyy-mm-dd hh24:mi:ss'),'星期日') from dual;        --返回:2022/2/20 16:07:43

7.round:对指定时间四舍五入

【函数格式】:round(dateTime[,dateFormat])

【函数说明】:该函数返回时间参数dateTime按格式参数dateFormat四舍五入后的结果,返回值为时间类型。

【参数说明】:参数dateTime为时间类型,参数dateFormat为字符型,该参数可选。

        参数dateFormat取值范围如下:

  • 省略该参数或该参数值为j时,表示取最近0点的日期;
  • 该参数值为day/dy/d时,表示取最近的星期日;
  • 该参数值为month/mon/mm/rm时,表示取最近的月初日期;
  • 该参数值为q时,表示取最近的季初日期;
  • 该参数值为syear/year/yyyy/yyy/yy/y(y的数量表示精度)时,表示取最近的年初日期;
  • 该参数值为cc/scc时,表示取最近的世纪初日期。

【样例展示一】:取最近0点的日期。

select round(to_date('2022/2/19 11:24:50','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:2022/2/19
select round(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:2022/2/20
select round(to_date('2022/2/19 11:24:50','yyyy-mm-dd hh24:mi:ss'),'j') from dual;        --返回:2022/2/19
select round(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'j') from dual;        --返回:2022/2/20

【样例展示二】:取最近的星期日。

select round(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'day') from dual;           --返回:2022/2/20
select round(to_date('2022/2/21 12:24:50','yyyy-mm-dd hh24:mi:ss'),'dy') from dual;             --返回:2022/2/20
select round(to_date('2022/2/25 12:24:50','yyyy-mm-dd hh24:mi:ss'),'d') from dual;               --返回:2022/2/27

【样例展示三】:取最近的月初日期。

select round(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'month') from dual;        --返回:2022/3/1
select round(to_date('2022/2/12 12:24:50','yyyy-mm-dd hh24:mi:ss'),'mon') from dual;           --返回:2022/2/1
select round(to_date('2022/2/16 12:24:50','yyyy-mm-dd hh24:mi:ss'),'mm') from dual;           --返回:2022/3/1
select round(to_date('2022/2/15 12:24:50','yyyy-mm-dd hh24:mi:ss'),'rm') from dual;             --返回:2022/2/1

【样例展示四】:取最近的季初日期。

select round(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'q') from dual;                 --返回:2022/4/1
select round(to_date('2022/1/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'q') from dual;               --返回:2022/1/1

【样例展示五】:取最近的年初日期。

select round(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'syear') from dual;        --返回:2022/1/1
select round(to_date('2022/6/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'year') from dual;                --返回:2022/1/1
select round(to_date('2022/7/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'yyyy') from dual;                --返回:2023/1/1
select round(to_date('2022/7/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'yyy') from dual;                --返回:2023/1/1
select round(to_date('2022/6/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'yy') from dual;                --返回:2022/1/1
select round(to_date('2022/7/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'y') from dual;                --返回:2023/1/1

【样例展示六】:取最近的世纪初日期。

select round(to_date('2022/1/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'cc') from dual;                --返回:2001/1/1
select round(to_date('1922/1/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'scc') from dual;                --返回:1901/1/1

8.trunc:对指定时间进行截取

【函数格式】:trunc(dateTime[,dateFormat])

【函数说明】:该函数返回时间参数dateTime对应时间格式dateFormat指定范围的第一天日期。

【参数说明】:参数dateTime为时间类型,参数dateFormat为字符型,该参数可选。

        参数dateFormat取值范围如下:

  • 省略该参数或改参数值为j: 表示取最近的0点日期;
  • 值为day/dy/d (周日为每周第一天):表示取最近的星期日;
  • 值为month/mon/mm/rm表示取最近的月初日期;
  • 值为q表示取最近的季初日期;
  • 值为syear/year/yyyy/yyy/yy/y(多个y表示精度):表示取最近的年初日期;
  • 值为cc/scc表示取最近的世纪初日期。

【样例展示一】:取最近的0点日期。

select trunc(to_date('2022/2/19 11:24:50','yyyy-mm-dd hh24:mi:ss')) from dual;                        --返回:2022/2/19
select trunc(to_date('2022/2/10 12:24:50','yyyy-mm-dd hh24:mi:ss')) from dual;                        --返回:2022/2/10
select trunc(to_date('2022/2/19 11:24:50','yyyy-mm-dd hh24:mi:ss'),'j') from dual;                        --返回:2022/2/19
select trunc(to_date('2022/2/10 12:24:50','yyyy-mm-dd hh24:mi:ss'),'j') from dual;                        --返回:2022/2/10

【样例展示二】:取最近的星期日。

select trunc(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'day') from dual;                --返回:2022/2/13
select trunc(to_date('2022/2/21 12:24:50','yyyy-mm-dd hh24:mi:ss'),'dy') from dual;                --返回:2022/2/20
select trunc(to_date('2022/2/25 12:24:50','yyyy-mm-dd hh24:mi:ss'),'d') from dual;                --返回:2022/2/20

【样例展示三】:取最近的月初日期。

select trunc(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'month') from dual;        --返回:2022/2/1
select trunc(to_date('2022/2/12 12:24:50','yyyy-mm-dd hh24:mi:ss'),'mon') from dual;                --返回:2022/2/1
select trunc(to_date('2022/2/16 12:24:50','yyyy-mm-dd hh24:mi:ss'),'mm') from dual;                --返回:2022/2/1
select trunc(to_date('2022/2/15 12:24:50','yyyy-mm-dd hh24:mi:ss'),'rm') from dual;                --返回:2022/2/1

【样例展示四】:取最近的季初日期。

select trunc(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'q') from dual;                --返回:2022/1/1
select trunc(to_date('2022/8/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'q') from dual;                --返回:2022/7/1

【样例展示五】:取最近的年初日期。

select trunc(to_date('2022/2/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'syear') from dual;                --返回:2022/1/1
select trunc(to_date('2022/6/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'year') from dual;                --返回:2022/1/1
select trunc(to_date('2022/7/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'yyyy') from dual;                --返回:2022/1/1
select trunc(to_date('2022/7/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'yyy') from dual;                --返回:2022/1/1
select trunc(to_date('2022/6/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'yy') from dual;                --返回:2022/1/1
select trunc(to_date('2022/7/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'y') from dual;                --返回:2022/1/1

【样例展示六】:取最近的世纪初日期。

select trunc(to_date('2022/1/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'cc') from dual;                --返回:2001/1/1
select trunc(to_date('1922/1/19 12:24:50','yyyy-mm-dd hh24:mi:ss'),'scc') from dual;                --返回:1901/1/1

9.extract:截取日期时间中的特定部分

【函数格式】:extract ( { year | month | day | hour | minute | second } | { timezone_hour | timezone_minute } | { timezone_region | timezone_abbr } from { date_value | interval_value } )

【函数说明】:该函数从时间类型参数中截取指定部分。

【样例展示一】:从date类型数据中进行截取,注意,只能从一个date类型数据中截取year、month、day。

select sysdate from dual;        --返回当前时间:2022/2/19 16:52:05
select extract (year from sysdate) from dual;        --返回:2022
select extract (month from sysdate) from dual;        --返回:2
select extract (day from sysdate) from dual;        --返回:19

【样例展示二】:从timestamp类型数据中进行截取。

select extract (year from to_timestamp('2022-02-19 17:03:35','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:2022
select extract (month from to_timestamp('2022-02-19 17:03:35','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:2
select extract (day from to_timestamp('2022-02-19 17:03:35','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:19
select extract (hour from to_timestamp('2022-02-19 17:03:35','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:17
select extract (minute from to_timestamp('2022-02-19 17:03:35','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:3
select extract (second from to_timestamp('2022-02-19 17:03:35','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:35
select systimestamp from dual;        --当前时间:19-2月 -22 05.08.34.810000 下午 +08:00
select extract (timezone_hour from systimestamp) from dual;       
 --返回:8
select extract (timezone_minute from systimestamp) from dual;        --返回:0
select extract (timezone_region from systimestamp) from dual;        --返回:UNKNOWN
select extract (timezone_abbr from systimestamp) from dual;        --返回:UNK

【样例展示三】:获取两个时间之间的时间间隔。

select extract (day from to_timestamp('2023-06-23 20:10:55','yyyy-mm-dd hh24:mi:ss') - to_timestamp('2021-08-20 11:15:30','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:672
select extract (hour from to_timestamp('2023-06-23 20:10:55','yyyy-mm-dd hh24:mi:ss') - to_timestamp('2021-08-20 11:15:30','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:8
select extract (minute from to_timestamp('2023-06-23 20:10:55','yyyy-mm-dd hh24:mi:ss') - to_timestamp('2021-08-20 11:15:30','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:55
select extract (second from to_timestamp('2023-06-23 20:10:55','yyyy-mm-dd hh24:mi:ss') - to_timestamp('2021-08-20 11:15:30','yyyy-mm-dd hh24:mi:ss')) from dual;        --返回:25

【样例展示四】:获取interval类型数据的指定部分

select extract(year from interval '50' year) year from dual;        --返回:50
select extract(month from interval '8' month) year from dual;        --返回:8
select extract(year from interval '50' month) year from dual;        --返回:4
select extract(month from interval '8' year) year from dual;        --返回:0

10.new_time:返回指定时区对应的时间

【函数格式】:new_time(dateTime, timeZone1, timeZone2)

【函数说明】:该函数返回时间dateTimetimeZone1时区对应timeZone2时区的日期和时间。

【参数说明】timeZone1timeZone2对应的时区简写如下:

  • 大西洋标准时间:AST或ADT
  • 阿拉斯加_夏威夷时间:HST或HDT
  • 英国夏令时:BST或BDT
  • 美国山区时间:MST或MDT
  • 美国中央时区:CST或CDT
  • 新大陆标准时间:NST
  • 美国东部时间:EST或EDT
  • 太平洋标准时间:PST或PDT
  • 格林威治标准时间:GMT
  • Yukou标准时间:YST或YDT

【样例展示】

select new_time(sysdate,'PDT','GMT') los_angles from dual;                                                   --返回:2022/2/19 22:16:24
select new_time(sysdate,'CST','YDT') los_angles from dual;                                                   --返回:2022/2/19 13:16:19
select new_time(sysdate,'NST','EDT') los_angles from dual;                                                   --返回:2022/2/19 14:46:48
select new_time(sysdate,'HST','ADT') los_angles from dual;                                                   --返回:2022/2/19 22:17:15

11.localtimestamp:返回会话中的日期和时间

【函数说明】:返回会话中的日期和时间,没有参数,不带括号,返回值为时间类型。

【样例展示】

select localtimestamp from dual;        --返回:19-2月 -22 05.29.31.442000 下午

12.current_timestamp:返回当前会话时区中的当前日期和时间

【函数说明】:以timestamp with time zone数据类型返回当前会话时区中的当前日期和时间,没有参数,不带括号,返回值为时间类型。

【样例展示】

select current_timestamp from dual;        --返回:19-2月 -22 05.32.56.402000 下午 +08:00

13.current_date:返回当前会话时区中的当前日期和时间

【函数说明】:返回当前会话时区中的当前日期和时间,没有参数,不带括号,返回值为时间类型。

【样例展示】

select current_date from dual;        --返回:2022/2/19 17:35:28

14.dbtimezone:返回时区

【函数说明】:返回时区,没有参数,不带括号,返回值为字符型。

【样例展示】

select dbtimezone from dual;        --返回:+00:00

15.sessiontimezone:返回会话时区

【函数说明】:返回会话时区,没有参数,不带括号,返回值为字符型。

【样例展示】

select sessiontimezone from dual;        --返回:+08:00

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值