Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 varchar to datetime

Author  Topic 

drl213
Starting Member

2 Posts

Posted - 2011-04-27 : 20:04:37
Hi guys,

I have a very simple (at least I think it's simple) question but I'm stuck and Google hasn't been able to help.

All I'm trying to do is change a varchar(50) column into datetime. All the values in that column are already formated to '1998-01-28 0:00:00' but every time I try I get an error stating: "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated."

Any help would be greatly appreciated!

Thanks,
Danny

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-27 : 20:52:22
Change the dateformat using the following command.
SET DATEFORMAT mdy; 
This change is effective only in the session you issued the command in, so do the alter table command in the same session.

If that does not work, see which rows are causing the problem using
select * from yourTable where isdate(yourvarcharcolumn) = 0
Go to Top of Page

drl213
Starting Member

2 Posts

Posted - 2011-04-27 : 21:28:56
Thanks! the isDate statement helped me isolate the bad fields. I fixed them and will test the query in the morning.

Thanks again!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-04-28 : 11:11:35
Also refer this
http://beyondrelational.com/blogs/madhivanan/archive/2010/06/03/understanding-datetime-column-part-ii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -