user3109653
user3109653

Reputation: 351

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?

Upvotes: 2

Views: 39346

Answers (3)

user2832577
user2832577

Reputation: 87

try

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

or

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

Upvotes: 0

Kaf
Kaf

Reputation: 33839

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

Upvotes: 6

T McKeown
T McKeown

Reputation: 12857

select DateAdd(second, someNbrCol, '1970-01-01')

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

Upvotes: 0

Related Questions