convert datetime from varchar error

A table in my database has date stored in varchar(50) column.The date is '22/09/2009 13:15:13'.How do i convert it to datetime.I am getting an error when i try to select CONVERT(datetime,'22/09/2009 13:15:13'):

Msg 242, Level 16, State 3, Line 4
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

more ▼

asked May 20, 2010 at 03:52 AM in Default

avatar image

Neha 1
35 6 7 10

(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

Use the third parameter to CONVERT, the style parameter.

For your particular style, you want to use style 103:

SELECT CONVERT(datetime,'22/09/2009 13:15:13', 103);

For more information, see the reference here: http://msdn.microsoft.com/en-us/library/ms187928.aspx

A good practice is always to use the ISO format of 'yyyymmdd' when using text dates, as that can't be mis-interpreted, and also sorts naturally in date order when using a text sort.

more ▼

answered May 20, 2010 at 04:55 AM

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

(comments are locked)
10|1200 characters needed characters left

Try this

set dateformat dmy
select CONVERT(datetime,'22/09/2009 13:15:13')

Hope this will help

more ▼

answered May 20, 2010 at 04:58 AM

avatar image

161 13 16 17

(comments are locked)
10|1200 characters needed characters left

Its because the value is being read in the format mm/dd/yyyy and there isnt a 22nd month. If you change the format of the supplied date it should be fine

convert(datetime, '20090922 13:15:13') --uses the ISO standard format of yyyymmdd

convert(datetime, '2009-Sep-22 13:15:13') --uses the the month name to avoid confusion but may have mixed results if you have any access that uses a non english month name
convert(datetime, '09/22/2009 13:15:13') -- uses the format expected by your installation

hope this helps

more ▼

answered May 20, 2010 at 04:33 AM

avatar image

Fatherjack ♦♦
43.8k 79 102 118

  • I shouldn't go for breakfast in the middle of answering posts... :)

May 20, 2010 at 04:57 AM Matt Whitfield ♦♦

breakfast?! Its nearly lunchtime. You Sir, are clearly not a horse owner. :D

May 20, 2010 at 05:00 AM Fatherjack ♦♦

No, just the 4 chickens, 3 cats, 2 gerbils and 1 dog... (!)

May 20, 2010 at 05:15 AM Matt Whitfield ♦♦

Hopefully well segregated to prevent it simply becoming the owner of 1 dog ... ?!

May 20, 2010 at 05:33 AM Fatherjack ♦♦

Well I think that depends on your perspective... Certainly the eggs are nice, but I think they're pets really. It was the Gypsy Fair Day in the village I live in today, had a narrow escape as the wife almost purchased a horse...

May 20, 2010 at 09:11 PM Matt Whitfield ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: May 20, 2010 at 03:52 AM

Seen: 2139 times

Last Updated: May 20, 2010 at 04:28 AM

Copyright 2018 Redgate Software. Privacy Policy