Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert int to date in SQL Server 2008

I am using MS SQL Server 2008, and in a table I have column idate as date but in the integer format. But in the query I want that in the date format. Is it possible to convert integer date into proper datetime format?

like image 669
Vikrant More Avatar asked Oct 20 '11 01:10

Vikrant More


People also ask

Can we convert integer to date in SQL?

The method will convert the integer value into dd/MM/yyyy format by extracting dd, MM, and yyyy parts separately using arithmetic operation and add / between MM and yyyy parts. It is then converted into VARCHAR datatype. CONVERT function with style 103 is used to convert the formatted string into a proper date value.

What is CAST () and convert () functions in SQL Server?

The cast and convert functions provide similar functionality. They are used to convert a value from one data type to another. So let's take a look at a practical example. The example is developed in SQL Server 2012 using the SQL Server Management Studio.


2 Answers

You can't convert an integer value straight to a date but you can first it to a datetime then to a date type

select cast(40835 as datetime)

and then convert to a date (SQL 2008)

select cast(cast(40835 as datetime) as date)

cheers

like image 150
davey Avatar answered Oct 07 '22 22:10

davey


You have to first convert it into datetime, then to date.

Try this, it might be helpful:

Select Convert(DATETIME, LEFT(20130101, 8))

then convert to date.

like image 31
Nikhil Gutha Avatar answered Oct 07 '22 23:10

Nikhil Gutha