Luke Luke - 4 months ago 7
SQL Question

get date from int (YYYYMMDD)

By doing a mistake a few months ago I got myself in a situation, where I have to find a workaround for the following problem:

I am saving dates as integer in a common format inside my sql database(YYYYMMDD). I want to have my select statement giving me the integer in a format, that looks like a date (or even is in a true date type) to the user, so i can use the recieved Datatable directly as datasource for my DataGridView.


  • I do not want to use clientside formatting

  • Let's call the datatable
    myTable
    and the integer/date colum
    myIntDate

  • myIntDate
    can be
    NULL

  • sample
    myIntDate-value
    : 20160803 for the 3rd of August 2016


Answer
Select cast(cast(20160729 as varchar(10)) as date)

Returns 2016-07-29

Comments