Question

Oracle SQL automatically converts my field D.START_DT to the following format:

TO_CHAR(D.START_DT,'YYYY-MM-DD')

Which makes it difficult for me to modify my own date format.

I've tried wrapping another TO_CHAR around it with no luck.

TO_CHAR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 'MM/DD')

And I've tried SUBSTR to select certain characters, with no luck. I think the hyphen is getting int he way.

SUBSTR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 6, 7) || '/' || SUBSTR(TO_CHAR(D.START_DT,'YYYY-MM-DD'), 9, 10)

What is the work around for this?

Was it helpful?

Solution 2

A DATE datatype has no format. When you see a date printed on a screen, there was something that APPLIED the format you see. Could be a "default" in the program you are using (like SQL Developer) or your NLS setting, etc. But, a DATE datatype has no format. So, you have complete control over the format you see on screen.

The simplest is to use the TO_CHAR function:

select TO_CHAR(D.START_DT,'YYYY') from dual;

returns just the four digit year.

See TO_CHAR date format options.

http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm#CDEHIFJA

You should always supply the format in your code and not rely on some other "default" to supply it.

OTHER TIPS

I agree with RMAN Express and see no problems converting dates to any format you need... In case you still have problems try this (first to_char() in outer query is optional):

SELECT to_char(to_date(some_date, 'YYYY-MM-DD'), 'MM/DD') final_date
  FROM
  (
  SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') some_date -- this is your "auto converted" date 
   FROM dual
  )
 /
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top