ORACLE常用時間查詢

2021-10-24 15:48:48 字數 4695 閱讀 2360

–取當前日期的本月末

select to_char(last_day(trunc(to_date(

20200903

,'yyyymmdd'),

'month'))

,'yyyymmdd'

) 本月末 from dual;

select to_char(last_day(trunc(to_date(

20200903

,'yyyymmdd'),

'mm'))

,'yyyymmdd'

) 本月末 from dual;

–取當前日期的上個月末

select to_char(last_day(add_months(to_date(

20200903

,'yyyymmdd'),

-1))

,'yyyymmdd'

) 上個月末 from dual;

select to_char(trunc(to_date(

20200903

,'yyyymmdd'),

'mm')-

1,'yyyymmdd'

) 上個月末 from dual;

–取當前日期的下個月末

select to_char(last_day(add_months(to_date(

20200903

,'yyyymmdd'),

1)),

'yyyymmdd'

) 下個月末 from dual;

–取當前日期的本月初

select to_char(trunc(to_date(

20200903

,'yyyymmdd'),

'mm'),

'yyyymmdd'

) 本月初 from dual;

–取當前日期的上個月初

select to_char(trunc(add_months(to_date(

20200903

,'yyyymmdd'),

-1),

'mm'),

'yyyymmdd'

) 上個月初 from dual;

–取當前日誌的下個月初

select to_char(trunc(add_months(to_date(

20200903

,'yyyymmdd'),

1),'mm'),

'yyyymmdd'

) 下個月初 from dual;

–取上個月的今天

select to_char(add_months(to_date(

20200903

,'yyyymmdd'),

-1),

'yyyymmdd'

) 上個月的今天 from dual;

–取去年今天

select to_char(add_months(to_date(

20200903

,'yyyymmdd'),

-12),

'yyyymmdd'

) 去年今天 from dual;

–取今年年末日期

select to_char(add_months(trunc(to_date(

20200903

,'yyyymmdd'),

'yyyy'),

12)-1

,'yyyymmdd'

) 今年末 from dual;

–取去年年末日期

select to_char(trunc(to_date(

20200903

,'yyyymmdd'),

'yy')-

1,'yyyymmdd'

) 去年年末 from dual;

–取去年年初日期

select to_char(trunc(to_date(

20200903

,'yyyymmdd'),

'yy')-

interval

'1'year

,'yyyymmdd'

) 去年年初 from dual;

–取當前日期

select to_char(to_date(

20200903

,'yyyymmdd'),

'yyyymmdd'

) 當前日期 from dual;

–取昨天日期

select  to_char(to_date(

20200903

,'yyyymmdd')-

1,'yyyymmdd'

) 昨天 from dual;

–取明天日期

select  to_char(to_date(

20200903

,'yyyymmdd')+

1,'yyyymmdd'

) 明天 from dual;

–取當前時間精確到5位毫秒

select to_char(systimestamp,

'yyyymmddhhmmssff5'

) 當前毫秒5

from dual;

–取當月中的每一天

select to_char(to_date(

20200401

,'yyyymmdd')+

level-1

,'yyyymmdd'

) dt from dual connect

bylevel

<= to_char(last_day(to_date(

20200401

,'yyyymmdd'))

,'dd'

);

select to_char(to_date(to_char(trunc(to_date(

20190203

,'yyyymmdd'),

'mm'),

'yyyymmdd'),

'yyyymmdd')+

level-1

,'yyyymmdd'

) dt from dual connect

bylevel

<= to_char(last_day(to_date(

20190203

,'yyyymmdd'))

,'dd'

);

–取當月到今天為止的每一天

select to_char(to_date(to_char(trunc(to_date(

20190203

,'yyyymmdd'),

'mm'),

'yyyymmdd'),

'yyyymmdd')+

level-1

,'yyyymmdd'

) dt from dual connect

bylevel

<= to_char(to_date(

20190203

,'yyyymmdd'),

'dd'

);

–指定週數,求當年該週的時間範圍

select

*from

(select

level

as week,

decode(sign(

5-decode(to_char(pm.sd,

'd')

,'1'

,'7'

,to_char(pm.sd,

'd')))

,-1,

next_day(pm.sd+

(level-1

)*7,

2),next_day(pm.sd+

(level-1

)*7-

7,2)

)as 當周第一天,

decode(sign(

5-decode(to_char(pm.sd,

'd')

,'1'

,'7'

,to_char(pm.sd,

'd')))

,-1,

next_day(pm.sd+

(level-1

)*7,

2),next_day(pm.sd+

(level-1

)*7-

7,2)

)+6as 當周最後一天

from dual d

left

join

(select trunc(to_date(

'2021-01-01'

,'yyyy-mm-dd'),

'yyyy'

)as sd from dual) pm on1=

1connect

bylevel

<=

53)dd

where dd.week =

33;

Oracle常用時間函式

設定時間格式alter session set nls date format dd mon yyyy hh mi ss 獲取系統時間1select sysdate from dual 獲取系統時間2select current date from dual 將日期轉為字串select to cha...

Oracle之常用時間語句

時間查詢或則根據各種需求需要查詢指定的時間,經常用到,就記錄下來,需要的朋友可以看下,希望對你們 有所幫助 1 當前時間 北京時間 2013.01.05 2 3 1.查詢本週第一天 4select trunc sysdate,d from dual 2012 12 30 星期天為第一天 5 2.查詢...

常用時間函式

比較常用的時間函式有time localtime asctime 和gmtime 函式time 的原型為 time t time time t time 函式time 返回系統的當前日曆時間,如果系統丟失時間設定,則函式返回 1。對函式time的呼叫,既可以使用空指標,也可以使用指向time t型別...