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)
 Cast varchar to datetime problem

Author  Topic 

SimonGough
Starting Member

23 Posts

Posted - 2002-01-17 : 07:39:36

I am trying to cast a varchar field to a datetime type so I can check to see if something is greater than one date and less than another date.

I am using: cast('17/01/02' as datetime)

but I get the following error:
The conversion of char data type to datetime data type resulted in an out-of-range datetime value.

Can anyone help me solve this??
Thnaks.


Nazim
A custom title

1408 Posts

Posted - 2002-01-17 : 07:51:44
your dateformat settings are in mdy(the default). therefore when you are trying to convert '17/01/02' to Datetime, it takes month as 17 which is out of rage.

Either you can Change your date settings or you can change the way you pass data to cast.

eg:

Set DateFormat ymd
or

cast('01/17/02' as datetime)

HTH

----------------------------------
"True love stories don't have endings."
Go to Top of Page

mphelps
Starting Member

12 Posts

Posted - 2002-01-17 : 07:56:48
converting from varchar to datetime is an implicit conversion. there is no need to use the cast or convert function

Martin

quote:
I don't suffer from insanity, I enjoy every second of it


Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2002-01-17 : 08:13:54
Tell it explicitly what format it is expecting:

select convert(datetime, '17/01/02', 3)


Go to Top of Page
   

- Advertisement -