1. ホーム
  2. データベース
  3. 神託

オラクルのTO_DATEの使い方解説

2022-01-18 15:02:52

TO_DATE形式(時刻:2007-11-02 13:45:25)

年です。

<ブロッククオート

yy 2桁 2桁 年表示値:07

yyy 3桁 3桁 年:007

{yyyy 4桁 yyyy 4桁 4桁 表示値:2007年

mm数 2桁の月 表示値:11

{

mon 省略文字集合の表示値。11月、英語の場合はnovを表示

{綴られた月が表示されます。 月スペルアウト文字セットは、値を示します。November、英語版であればNovember。

日です。

dd番号 月日 表示値:02

ddd number 1年の最初の日 表示値:02

dy abbreviated 1週間の日数の略称 値が表示されます。金曜日、英語ならfriと表示されます。

{曜日指定 day spelled out 完全な曜日 値が表示されます。金曜日、英語の場合はfriday 

時間

hh 2桁 12時間表示 バイナリ 表示値:01

hh24 2桁の24時間バイナリ 表示値:13

mi 2桁 60進数 表示値:45

2番目

ss 2桁 60進数 表示値:25

その他

Q桁の四半期 表示値:4

WW digit 1年の週 表示値:44

W digit 今月の週 表示値:1

24時間制の時間範囲。0:00:00 - 23:59:59 ....

12時間表示の時間範囲。1:00:00 - 12:59:59 ....

1. 日付・文字変換機能の使い方(to_date,to_char)

    
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //convert date to string  
select to_char(sysdate,'yyyy') as nowYear from dual; //get the year of the time  
select to_char(sysdate,'mm') as nowMonth from dual; //Get the month of the time  
select to_char(sysdate,'dd') as nowDay from dual; //Get the day of the time  
select to_char(sysdate,'hh24') as nowHour from dual; //Get the time in hours  
select to_char(sysdate,'mi') as nowMinute from dual; //Get the minutes of the time  
select to_char(sysdate,'ss') as nowSecond from dual; //get the time in seconds

2. 文字列と時刻の交換

select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual
select to_char( to_date(222,'J'),'Jsp') from dual //Show Two Hundred Twenty-Two    

3. ある日が何曜日かを調べる

select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual; //Monday     
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day',
'NLS_DATE_LANGUAGE = American') from dual; // monday   
// Set the date language     
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';     
// You can also do this     
TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')    

4. 2つの日付の間の日数

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

5. 時刻をNULLで使用する

select id, active_date from table1     
UNION     
select 1, TO_DATE(null) from dual; //Note that TO_DATE(null) should be used    

6. 月差

a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')     
//then after 12:00 noon on December 31 and before 12:00 on December 1 are not included in this range.     
//So, when the time needs to be precise, to_char is still considered necessary

7. 日付フォーマットの衝突問題

入力形式は、インストールされているORACLEの文字セットの種類によって異なり、例えば以下のようになります。US7ASCII の場合、日付フォーマットのタイプは次のようになります: '01-Jan-01'

alter system set NLS_DATE_LANGUAGE = American     
alter session set NLS_DATE_LANGUAGE = American     
//or write in to_date     
select to_char(to_date('2002-08-26','yyyy-mm-dd'),
   'day','NLS_DATE_LANGUAGE = American') from dual;     
// Note that I've only given NLS_DATE_LANGUAGE here, but there are many more, so check out     
select * from nls_session_parameters     
select * from V$NLS_PARAMETERS    

8.特殊条件日のクエリ

select count(*)     
from ( select rownum-1 rnum     
   from all_objects     
   where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-     
   02-01','yyyy-mm-dd') + 1     
  )     
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )     
    not in ( '1', '7' )     
//find the number of days between 2002-02-28 and 2002-02-01 except Monday and seven     
//GET_TIME, and then subtract the result (1/100th of a second, not milliseconds).    

9. 月を探す

select months_between(to_date('01-31-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;     
// the result is: 1     
select months_between(to_date('02-01-1999','MM-DD-YYYY'),
to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;     
//result is: 1.03225806451613

10. next_dayの使用法

Next_day(date, day)     
Monday-Sunday, for format code DAY     
Mon-Sun, for format code DY     
1-7, for format code D    

11. 時間数を取得する

//extract() to find the field value of the date or interval value
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer     
select sysdate ,to_char(sysdate,'hh') from dual;     
 
SYSDATE TO_CHAR(SYSDATE,'HH')     
-------------------- ---------------------     
2003-10-13 19:35:21 07     
select sysdate ,to_char(sysdate,'hh24') from dual;     
 
SYSDATE TO_CHAR(SYSDATE,'HH24')     
-------------------- -----------------------     
2003-10-13 19:35:21 19    

12. 年・月・日の取り扱い

SELECT
  older_date,
  newer_date,
  years,
  months,
  ABS (
    TRUNC (
      newer_date - ADD_MONTHS (older_date, years * 12 + months)
    )
  ) days
FROM
  (
    SELECT
      TRUNC (
        MONTHS_BETWEEN (newer_date, older_date) / 12
      ) YEARS,
      MOD (
        TRUNC (
          MONTHS_BETWEEN (newer_date, older_date)
        ),
        12
      ) MONTHS,
      newer_date,
      older_date
    FROM
      (
        SELECT
          hiredate older_date,
          ADD_MONTHS (hiredate, ROWNUM) + ROWNUM newer_date
        FROM
          emp
      )
  )   

13. 月の日数が変動する場合の考え方

select to_char(add_months(last_day(sysdate) + 1, -2), 'yyyymmdd'),last_day(sysdate) from dual    

14. 1年の日数を求める

select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual    
   // leap year processing method     
  to_char( last_day( to_date('02' | :year,'mmyyyy') ), 'dd' )     
   //If it's 28, it's not a leap year 
 

15. yyyy と rrrr の違い

   YYYY99 TO_C     
   ------- ----     
   yyyy 99 0099     
   rrrr 99 1999     
   yyyy 01 0001     
   rrrr 01 2001   

16. 異なるタイムゾーンの扱い

 select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,
   sysdate from dual;    

17. 5秒間隔

   Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSSS')/300) * 300,'SSSSSS') ,
   TO_CHAR(sysdate,'SSSSSS') from dual    
   //2002-11-1 9:55:00 35786     
   //SSSSS is a 5-digit number of seconds    
 

18. 日

    select TO_CHAR(SYSDATE,'DDD'),sysdate from dual   
   //310 2002-11-6 10:03:51    
 

19. 時間、分、秒、ミリ秒を計算する

     SELECT
      Days,
      A,
      TRUNC (A * 24) Hours,
      TRUNC (A * 24 * 60 - 60 * TRUNC(A * 24)) Minutes,
      TRUNC (
        A * 24 * 60 * 60 - 60 * TRUNC (A * 24 * 60)
      ) Seconds,
      TRUNC (
        A * 24 * 60 * 60 * 100 - 100 * TRUNC (A * 24 * 60 * 60)
      ) mSeconds
    FROM
      (
        SELECT
          TRUNC (SYSDATE) Days,
          SYSDATE - TRUNC (SYSDATE) A
        FROM
          dual
      ) SELECT
        *
      FROM
        tabname
      ORDER BY
        DECODE (MODE, 'FIFO', 1 ,- 1) * TO_CHAR (rq, 'yyyymmddhh24miss')
      
   // floor((date2 - date1) /365) as year     
   // floor((date2-date1, 365) /30) as month     
   // d(mod(date2-date1, 365), 30) as day.
 

20. next_day関数

   //return the next week's date, day is 1-7 or Sunday-Saturday, 1 means Sunday
   next_day(sysdate,6) is the next Friday from the current one. The next number is counted from Sunday.     
   // 1 2 3 4 5 6 7     
   // day one two three four five six   
   select (sysdate-to_date('2003-12-03 12:55:45','yyyy-mm-dd hh24:mi:ss'))*24*60*60 from dual
   //the date is returned as a day and then converted to ss

21,round[直近の日付に丸める](日:直近の日曜日に丸める)

   select sysdate S1,
   round(sysdate) S2 ,
   round(sysdate,'year') YEAR ,
   round(sysdate,'month') MONTH ,
   round(sysdate,'day') DAY from dual

22,trunc[truncate to the nearest day, in days] , 日付の種類を返します。

   select sysdate S1,                    
     trunc(sysdate) S2, //returns the current date, without hours, minutes and seconds
     trunc(sysdate,'year') YEAR, //returns the current year's January 1, no hours, minutes and seconds
     trunc(sysdate,'month') MONTH , //return to the current month of 1 day, no time minutes and seconds
     trunc(sysdate,'day') DAY //return to the current week's Sunday, no hours, minutes and seconds
   from dual

23,日付リストで最新の日付を返す

   select greatest('01-Jan-04','04-Jan-04','10-Feb-04') from dual


24. 時差を計算する

     Note: oracle time difference is in days, so convert to adult months, days
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual //time difference - year
      select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual //time difference - month
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual //time difference - days
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual //time difference - hours
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual //time-difference-minutes
      select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
      'yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual //time-difference-seconds
 

25. 更新時間

 //oracle time is added and subtracted in days, set the amount of change to n, so convert to adult months, days
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
    to_char(sysdate+n*365,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //change time - year
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     add_months(sysdate,n) as newTime from dual //change time - month
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //change time-of-day
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     to_char(sysdate+n/24,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //change time-time
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     to_char(sysdate+n/24/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //change time-minute
 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
     to_char(sysdate+n/24/60/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //change time-seconds
 

26. 月の初日と最終日を探す

     SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month,
       Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month,
       Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month,
       LAST_DAY(Trunc(