Luke Luke - 6 months ago 17
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
    and the integer/date colum

  • myIntDate
    can be

  • sample
    : 20160803 for the 3rd of August 2016

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

Returns 2016-07-29