简体   繁体   中英

What is the difference between 'YYYY' and 'RRRR' in Oracle SQL

I have 2 queries in SQL:

select trunc(to_date('27-Jul-1987'),'YYYY') FROM dual; 

and

select trunc(to_date('27-Jul-1987'),'RRRR') FROM dual; 

Both are giving me the same result. What is the difference between 'RRRR' and 'YYYY'?

YYYY gives the current year as 4 digits.

RRRR format means 2-digit years in the range 00 to 49 are assumed to be in the current century (ie have the same first two digits as the current year), and years given as 50 through 99 are assumed to be in the previous century.

If the first 2 digits for the year are not specified in the date to be converted:

  • YYYY will always return the current year.
  • RRRR returns the year based on the current specified year in the database.

Try this sample code:

SELECT TO_DATE ('010199', 'MMDDYYYY') AS date_a,
       TO_DATE ('010199', 'MMDDYY') AS date_b,
       TO_DATE ('010199', 'MMDDRR') AS date_c,
       TO_DATE ('010199', 'MMDDRRRR') AS date_d
  FROM DUAL;

The results when run on 12/01/2014:

 DATE_A       DATE_B       DATE_C        DATE_D   
 ---------    ---------    ---------     ---------  
 1/1/0099      1/1/2099     1/1/1999      1/1/1999

This oracle link gives a great description and examples.

From the above link :

  • If the specified two-digit year is 00 to 49, then
    • If the last two digits of the current year are 00 to 49, then the returned year has the same first two digits as the current year.
    • If the last two digits of the current year are 50 to 99, then the first 2 digits of the returned year are 1 greater than the first 2 digits of the current year.
  • If the specified two-digit year is 50 to 99, then
    • If the last two digits of the current year are 00 to 49, then the first 2 digits of the returned year are 1 less than the first 2 digits of the current year.
    • If the last two digits of the current year are 50 to 99, then the returned year has the same first two digits as the current year.

This:

SQL> select  to_char(to_date('72-01-01','rrrr-mm-dd'),'yyyy') from dual;

will give you:

1972

But this:

SQL> select  to_char(to_date('72-01-01','yyyy-mm-dd'),'yyyy') from dual;

will give you:

0072

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM