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.

Fatherjack avatar image Fatherjack ♦♦ commented ·
+1 - congrats on 3k Jonlee :D
0 Likes 0 ·
Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
Congrats sir :)
0 Likes 0 ·
sp_lock avatar image sp_lock commented ·
Cheers chaps... Any of you going to the Event on the 17th in London? http://sqlblogcasts.com/blogs/simons/archive/2010/05/14/paul-and-kimberly-are-coming-the-uk.aspx
0 Likes 0 ·
Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
@Jonlee - no - I'm really busy at the moment - it's sucking quite a lot!
0 Likes 0 ·
David Wimbush avatar image David Wimbush commented ·
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.