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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 problem with column datatype

Author  Topic 

fogofogo
Starting Member

11 Posts

Posted - 2008-10-30 : 05:45:35
Hello,

I'm trying to change a column datatype in a ms sql 2000 database, that contains 700k records. Its currently set as a varchar, and I need to change it to a datetime format. The dates in the column are set as dd/mm/yyyy ( 20/02/2007).

When I try to change it I get this error...

ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
[Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated.


So obviously there is dodgy date in one of the rows. Does anyone know any sql function that I can use to run and select the records with the date errors?

Thanks

Matt

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-30 : 06:15:40
you may use this
select * from yourtable where isdate(datefield)=0
or (isdate(datefield)=1 and len(datefield)<8)
Go to Top of Page

fogofogo
Starting Member

11 Posts

Posted - 2008-10-30 : 06:56:26
Thank you.

Here is what I have discoved...

The dates that are ok are in this format dd/mm/yyyy, but the dates with the errors are mm/dd/yyyy

So, 12/10/2008 is fine, where 13/10/2008 is not.

Thousands of records are in the wrong format - is there anyway I can change them using sql?

Thanks again
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-10-30 : 07:10:03
Assuming you don't have mixed Dates in your table.
Assuming they're always dd/mm/yyyy.

set language german
select convert(datetime,'13/12/2008')

and maybe then...
set language english

Webfred


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-30 : 08:20:59
Always use proper datetime datatype to store dates

set dateformat dmy
--do process
set dateformat mdy


Madhivanan

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

- Advertisement -