I have statement which is comparing a smalldatetime column to literal string as follows:
sales_date ='21-9-2004 0:0:0.000' 
when I run the statement in query analyzer it bombs out with:
Server: Msg 296, Level 16, State 3, Line 1
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.
If I alter the format of the date literal to '2004-09-21 00:00:00' the statement works.
Is there anyway of forcing the statement to treat '21-9-2004 0:0:0.000'  as '2004-09-21 00:00:00'  without modifying the statement itself ?There might be a global setting for how datetime fields are treated by default, I've never been tempted to go look for it. I'd rather do a CONVERT instead. There's also a 'SET DATEFORMAT' that might work for you.
What's wrong with changing the statement?|||Where is the data coming from?  Is it always in that format?  Can you use SUBSTRING?|||Unfortunately the data is in a liternal string 'DD-MM-YYYY' when in fact I require it be in 'MM-DD-YYYY' format.|||Couldn't you do something like...
cast(day(sales_date()) as varchar(2)) + '-' + 
 cast(month(sales_date()) as varchar(2)) + '-' + 
 cast(year(sales_date()) as char(4)) + ' 0:0:0.000'
??
 
No comments:
Post a Comment