0

I have table name bill_date and this is my table row, the field type of create_date is character varying

id | create_date 
1  | 20080108
2  | 20080116

I want to convert create_date to Date but when I used this QUERY

SELECT to_date(create_date, 'YYYYddmm'),* FROM bill_date 

I got wrong result

2008-08-01
2009-04-03

there something wrong in field type? any help. thanks

5
  • show your expected output Commented Apr 16, 2015 at 5:27
  • I suppose you would like the output to be 2008-01-08? Can I suggest you leave it in the format given to you by to_date, as this is ISO format? Commented Apr 16, 2015 at 5:29
  • I suppose you would like the output to be 2008-01-08? yes, and row 2 also wrong output. Commented Apr 16, 2015 at 5:30
  • have you tried select '20080108'::date Commented Apr 16, 2015 at 5:31
  • 1
    The output of 2008-08-01 for the format YYYYddmm absolutely correct for the input value 20080108. 2008 = YYYY, 01 = dd, 08 = mm. If you want the input value to be parsed differently you need to use a different format mask, e.g. yyyymmdd Commented Apr 18, 2015 at 11:42

2 Answers 2

1
SELECT '20080108'::DATE
    ,to_date('20080108', 'YYYYddmm')

result

date         to_date    
----------   ---------- 
2008-01-08   2008-08-01 
Sign up to request clarification or add additional context in comments.

Comments

0
ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE using to_date(<columnname>,'YYYYDDMM');

Comments

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.