Converting Integer to Date

I am trying to convert an integer datatype into a date. Here is the coding that I have so far:

SELECT CONVERT(column_name, yyyymm) from table_name; 

What do I need to add?

6

3 Answers

Assuming your integer column (say, your_column) is representing year and month in yyyymm format, this should work.

First, convert your int column to a varchar and then add '01' to make it yyyymmdd (ISO Format), then convert to datetime/date.

SELECT CONVERT(date, CONVERT(varchar(6), your_column) + '01') myDate FROM TableName 
5
select DateAdd(second, someNbrCol, '1970-01-01') 

The last argument you would have to provide as the starting date.

try

SELECT CONVERT(date, convert(char(8),20180521)) 

or

select FORMAT(CONVERT (date,convert(char(8),20180521)),'dd-MMM-yyyy') 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like