donviti
donviti

Reputation: 294

Convert an Int to a date field

I'm trying to convert an integer field to an actual date field. Someone created a "date" field that just sticks in a "date" that is actually an integer. I'm trying to convert it to an actual date.

I have tried the following to no avail:

CAST(CAST(last_purch_date AS CHAR) AS DATE) as Create,
CAST( last_purch_date as datetime) as Created,
convert(datetime,last_purch_date) as Created1,
ISDATE(CONVERT(CHAR(8),last_purch_date)) as PleaseDearGodWORK

Upvotes: 1

Views: 27483

Answers (2)

Chandana Kumara
Chandana Kumara

Reputation: 2655

SELECT convert(date,CONVERT(varchar(8),[columname],101))

Upvotes: 1

John Cappelletti
John Cappelletti

Reputation: 82010

Simple cast as date could work

Select cast(cast(20161011 as varchar(8)) as date)

Returns

2016-10-11

If your data is suspect, you could also use Try_Convert()

Select Try_Convert(date,cast(2610 as varchar(8)))

Returns

NULL

Upvotes: 5

Related Questions