question

suraj avatar image
suraj asked

how to convert char value to datetime

i have varchar col in the table which store the date in the format '22-Jul-2009 12:35:29.998'

when i use convert(datetime,qdate) or cast(calldate AS smalldatetime) in sql statement

it gives an error Syntax error converting datetime from character string.

t-sqldate-functionsdata-conversion
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sp_lock avatar image
sp_lock answered

The syntax below is correct. What version of MSSQL are you using?

DECLARE @qdate VARCHAR(25)
SET  @qdate = '22-Jul-2009 12:35:29.998'
SELECT cast (@qdate AS SMALLDATETIME )

as does

DECLARE @qdate VARCHAR(25)
SET @qdate = '22-Jul-2009 12:35:29.998'
SELECT convert (SMALLDATETIME, @qdate )
5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

+1 - congrats on 3k Jonlee :D
0 Likes 0 ·
Congrats sir :)
0 Likes 0 ·
@Jonlee - no - I'm really busy at the moment - it's sucking quite a lot!
0 Likes 0 ·
Hi Jonlee, I'm going. See you there?
0 Likes 0 ·
Fatherjack avatar image
Fatherjack answered

I think you will only get answers that are the same as the answers to your other (very similar) question here http://ask.sqlservercentral.com/questions/6636/what-is-the-best-method-to-use-isdate-in-sql

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.