Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

sql server - ddmmyyyy to sql datetime in SQL

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...