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...
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?
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
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')