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?
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With