DB2 Convert from YYYYMMDD to Date

56,025

SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as "MyDate"

Share:
56,025
Chry007
Author by

Chry007

Programmer and Consultant at mid to large scale IT-Projects in Germany.

Updated on September 16, 2020

Comments

  • Chry007
    Chry007 over 3 years

    I have a column that stores a date as char in the format 'YYYYMMDD'. Now I want to convert it to a real date. I tried

    select cast (DATEFIELD as DATE) as MyDate
    

    But it only returns the old YYYYMMDD format labeled as 'DATE'. What am I doing wrong?

  • Chry007
    Chry007 almost 9 years
    I tried your second suggestion but I get: NO AUTHORIZED CONVERT NAMED HAVING COMPATIBLE ARGUMENTS WAS FOUND SQL Code: -440, SQL State: 42884
  • Asha
    Asha about 7 years
    this answer will alter the data table where the question is about how to get the data column converted.
  • Hogan
    Hogan over 6 years
    This is sql server not db2
  • Asha
    Asha over 6 years
    @Hogan: The answer has been edited since my comment, Please take a look at edits. My commnet was relevant when there was an Alter statement.
  • Gnqz
    Gnqz over 6 years
    @Hogan The question was also different, I really changed my answer since it was not relevant to the question anymore. Sorry for misguiding you.
  • Hogan
    Hogan over 6 years
    @Asha -- ugh sorry about that, I didn't look at the history.
  • Paul
    Paul almost 6 years
    On the iSeries, I'd sometimes have problems with TIMESTAMP_FORMAT and instead use TO_DATE