Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ddmmyyyy to sql datetime in SQL

Tags:

sql

sql-server

I need to convert a nvarchar value to datetime in T-SQL. The value is in ddmmyyyy format, e.g. 23072009

I need to convert to datetime in T-SQL.

I tried

select convert(datetime, '23072009', 103)

But it is throwing error.

"The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value."

Any idea

Thanks

like image 475
user1557886 Avatar asked Aug 20 '12 09:08

user1557886


1 Answers

Rebuild your format to yyyymmdd.

declare @D varchar(8)
set @D = '23072009'

select cast(right(@D, 4)+substring(@D, 3, 2)+left(@D, 2) as datetime)
like image 100
Mikael Eriksson Avatar answered Oct 05 '22 11:10

Mikael Eriksson