issue with to_date function with sysdate

前端 未结 2 1377
夕颜
夕颜 2020-12-12 00:37

I saw this question so I\'ve one doubt regarding this question: I am getting different result with the same date on the following query.

SELECT TO_CHAR(to_da         


        
2条回答
  •  庸人自扰
    2020-12-12 01:13

    I want to explain why you get different results.

    See this sqlfiddle

    As it is already said, sysdate is seen as DATE type and you are doing an implicit conversion when

    select to_date(sysdate, format) from dual;
    

    because first parameter of to_date should be varchar type the system does:

    select to_date(to_char(sysdate), format) from dual;
    

    because your implicit date format is 'DD-MON-YY', your query goes into:

    SELECT TO_CHAR(to_date('01-JAN-13', 'DD-MON-yy'), 'DAY'),
      TO_CHAR(to_date('01-JAN-13', 'DD-MON-yyyy'), 'DAY'),
      TO_CHAR(to_date('01-JAN-13', 'DD-MON-rr'), 'DAY'),
      TO_CHAR(to_date('01-JAN-13', 'DD-MON-rrrr'), 'DAY')
    FROM dual;
    

    the second to_date, because yyyy is a full thousands year format, goes to '01-JAN-0013' which is 13AD and probably is SUNDAY :)

提交回复
热议问题