6

I am trying to convert a character varying date field to the format YYYYMMDD and below is the select query I tried. The output format is 1999-04-27 (YYYY-MM-DD). Admindate is the field name in the table.

select to_date( admindate,'MMDDYYYY')  from test;

Can someone please advice what I am doing wrong?

4
  • Is admindate of type varchar? If so, In what format is the date stored? Commented Feb 14, 2017 at 20:01
  • Yes, Admindate is a varchar and it is srored as MMDDYYYY. Commented Feb 14, 2017 at 20:03
  • Oops, Sorry for the wrong question. I am trying the change the format to YYYYMMDD. Commented Feb 14, 2017 at 20:04
  • 3
    The real question is: why on earth are you storing a date in a varchar column? You should fix that as soon as possible Commented Feb 14, 2017 at 20:17

2 Answers 2

8

Use to_char to convert the converted date into the desired format.

select to_char(to_date(admindate, 'MMDDYYYY'), 'YYYYMMDD')
from test;
Sign up to request clarification or add additional context in comments.

Comments

5

you can try this

SELECT TO_CHAR(NOW() :: DATE, 'dd/mm/yyyy');

1 Comment

Please add a short explanation to your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.