Matt Seymour
Matt Seymour

Reputation: 9395

Converting a number to datetime sql

I have the number 20080331.

I need to cast/convert this into a datetime so I can do a date comparison within the database. How will i go about converting this number. Using CONVERT(DATETIME, Value) does not seem to work.

Upvotes: 4

Views: 20597

Answers (2)

Quassnoi
Quassnoi

Reputation: 425251

SELECT  CONVERT(DATETIME, CAST(20110331 AS VARCHAR(8)), 112)

Upvotes: 5

Alex K.
Alex K.

Reputation: 175748

You need to cast to a character type first;

select cast(cast(20080331 as varchar(8)) as datetime)

>>2008-03-31 00:00:00.000

Upvotes: 4

Related Questions