Oracle查詢語句對日期的處理

2021-10-07 01:45:42 字數 1900 閱讀 5703

-- oracle時間格式處理(字串處理)

select sysdate from dual --返回當前日期+時間

-- trunc 是用來擷取的

select trunc(sysdate) from dual --返回當前日期

select trunc(sysdate,'dd') from dual --返回當前年月日

select trunc(sysdate, 'year') from dual -- 返回當年第一天

select trunc(sysdate,'yy') from dual--返回當年第一天

select trunc(sysdate,'yyyy') from dual--返回當年第一天

select trunc(sysdate, 'q') from dual --擷取到季度(當前季度的第一天)

select trunc(sysdate, 'month') from dual --返回當月第一天

select trunc(sysdate,'mm') from dual--返回當月第一天

select trunc(sysdate,'d') from dual  --返回當前星期的第一天

select trunc(sysdate, 'hh') from dual  --當前時間,精確到小時

select trunc(sysdate, 'mi') from dual  --當前時間,精確到分鐘   

-- select trunc(sysdate, 'ss') from dual  -- 沒有這種寫法,trunc()函式沒有秒的精確

-- to_char 字串轉化

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowtime from dual   --日期轉化為字串  

select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') as nowtime from dual         --日期(24小時)

select to_char(sysdate,'yyyy') as nowyear   from dual   --年  

select to_char(sysdate,'mm')  as nowmonth from dual   --月  

select to_char(sysdate,'dd')  as nowday from dual   --日  

select to_char(sysdate,'hh24') as nowhour   from dual  --時  

select to_char(sysdate,'mi')    as nowminute from dual  --分  

select to_char(sysdate,'ss')    as nowsecond from dual   --秒

-- to_date 轉化成時間

select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual

-- 根據日期求星期 monday   

select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual 

-- 日期相隔的天數

select floor(sysdate - to_date('20020405','yyyymmdd')) from dual

-- 查詢月份的差值

select  months_between( to_date('20011201','yyyymmdd') , to_date('20011231','yyyymmdd') ) from dual

-- 查詢一年的第幾天

select to_char(sysdate,'ddd'),sysdate from dual 

ORACLE對日期的操作

select to char trunc sysdate,mm yyyymmdd from dual 月1號 select to char last day sysdate yyyymmdd from dual 月末 select to char trunc sysdate,iw yyyymmdd ...

Oracle 的TO DATE對日期處理

用例 select to date 2014 09 03 yyyy mm dd hh24 mi ss as nowtime from dual 1.日期和字元轉換函式用法 to date,to char select to char sysdate,yyyy mm dd hh24 mi ss as ...

mysql對日期的操作 MySql對日期的操作

1 計算倆個日期之間所差的天數 2 計算日期是這週的星期幾 select dayofweek 2018 09 18 1.因為外國一般都是把星期天認為是一周的開始,所以用這種方法一般都得減去一天才是這週的星期幾 2.這種方法是直接獲取當天是週幾 s,s 兩位數字形式的秒 00,01,59 i 兩位數字...