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
 Transact-SQL (2000)
 using cast and can not get it to work

Author  Topic 

csphard
Posting Yak Master

113 Posts

Posted - 2002-01-07 : 13:37:12
I'm working with a date field that has the data type of varchar. I am trying to use the datepart function against it but have been getting the following error.

The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The following represents my code can anyone tell me whats wrong with it.


select birth_date from emp_information where datepart(yy,( cast(birth_date as datetime))) = '1965'



nr
SQLTeam MVY

12543 Posts

Posted - 2002-01-07 : 13:43:49
You have at least one birth_date that cannot be converted to a datetime.
Try looking at the data.
You may have to put a style in the conversion if the format is something like
dd/mm/yyyy
yyyy-mm-dd

but not if it is
yyyymmdd.

==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page

davidpardoe
Constraint Violating Yak Guru

324 Posts

Posted - 2002-01-08 : 05:07:52
Also, bear in mind that the datetime data type does not support dates before 1753!

I have had to eliminate these in previous situations using a case statement (despite any date before 1753 being clearly incorrect!)

============
The Dabbler!
Go to Top of Page
   

- Advertisement -