x

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

Neha 1 gravatar image

Neha 1
35 5 7 8

(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

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.4k 61 65 87

gah, the style parameter, good catch.
May 20, 2010 at 04:58 AM Fatherjack ♦♦
Thank u ..that was quick..
May 20, 2010 at 08:51 PM Neha 1
(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

suraj gravatar image

suraj
161 12 14 14

Thanks that helped..
May 20, 2010 at 08:51 PM Neha 1
(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

Fatherjack gravatar image

Fatherjack ♦♦
42.3k 75 78 108

+1 - 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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x94
x37

asked: May 20, 2010 at 03:52 AM

Seen: 1866 times

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