issue with to_date function with sysdate

被刻印的时光 ゝ 提交于 2019-11-28 14:12:14

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 :)

SYSDATE is already a date. So if you write:

TO_DATE(SYSDATE, 'DD-MON-yy')

you are already doing two conversions, an implict one from date to string and an explicit one from string to date. The implicit one is the problem as you can't specify the date format. (Instead it's taken from the session's current settings.)

So the solution is to get rid of the unnecessary conversion an just write:

SELECT TO_CHAR(SYSDATE, 'DAY') FROM DUAL;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!