Geetha
Geetha

Reputation: 199

Sql Server datetime conversion

I am trying to convert varchar date value to datetime format. Showing error

select CONVERT(DATETIME, Convert(varchar, 20/12/2009, 103 ),103)

error: Conversion failed when converting datetime from character string.

Geetha

Upvotes: 0

Views: 176

Answers (4)

Oded
Oded

Reputation: 499002

You need to quote the date string:

select CONVERT(DATETIME, Convert(varchar, '20/12/2009', 103 ),103)

And you don't need two converts. This is should be enough:

select Convert(varchar, '20/12/2009', 103 )

Upvotes: 0

Rich
Rich

Reputation: 3123

You need quotes around the string otherwise it treats it as an expression and tries to do the divisions:

select CONVERT(DATETIME, Convert(varchar, '20/12/2009', 103 ),103)

Although having said that only one Convert is really necessary so you can use

select CONVERT(DATETIME, '20/12/2009',103)

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 401002

I would say you at least have to add quotes arround the string representing the date :

select CONVERT(DATETIME, Convert(varchar, '20/12/2009', 103 ),103)

(Maybe there's another problem too : I don't have an SQL Server instance available, so I cannot test)

Upvotes: 0

David M
David M

Reputation: 72870

This should work:

select CONVERT(DATETIME, '20/12/2009', 103)

Not sure what your conversion TO varchar is for...

Upvotes: 1

Related Questions