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.
| Author |
Topic |
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2005-07-13 : 17:40:21
|
| Hi friendswhats wrong with following query it returns incorrect resultsdeclare @x varchar(15)set @x= '''03/11/2003'''select * from task where convert(varchar(10),task.entrydt,103) >= @xit works only if i doselect * from task where convert(varchar(10),task.entrydt,103) = @xbut i want to be able to do >,>=, <= on datesThanks for any ideasCheers |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2005-07-13 : 19:37:04
|
If you want to compare dates with operators you must compare them as date datatypes.As your query is currently, you arer comparing two strings, right?Youll see what I mean, look at how these dates are ordered when converted to varchar datatype:declare @date table (testDate datetime)insert into @date select getdate() union select getdate()-5 union select getdate()-10 union select '03/11/2003'select convert(varchar(10),testDate,103)from @date order by 1select testDatefrom @date order by 1 @x needs to either be a datetime, or converted to one to be compared against another datetime (task.entrydt).Nathan Skerl |
 |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2005-07-13 : 19:43:54
|
| What are the resulting that you are getting versus the results you desire?Note that you say you want to do comparisons on dates but you are converting the data to a string and comparing it to another string. Perhaps what you want to do is convert the @x string into a datetime and leave the datetime date as a datetime data item.Ex.select *from taskwhere task.EntryDate >= Convert(datetime, @x)As a minor observation, I avoid using double quotes as a string delimiter. A change in your server configuration could yield unexpected systax errors since that change could imply that quoted strings are actually identifiers. Use single quotes instead.HTH=================================================================The most tyrannical of governments are those which make crimes of opinions, for everyone has an inalienable right to his thoughts. -Baruch Spinoza, philosopher (1632-1677) |
 |
|
|
rajani
Constraint Violating Yak Guru
367 Posts |
Posted - 2005-07-13 : 21:06:48
|
| Thanks Nathan ,Bustaz for ur repliesi changed my variable to a datetime field and tried following.i have 2 records with following dates2004-06-29 16:32:08.0002004-06-29 12:37:18.000when i run following query declare @x datetimeset @x= '29/06/2004'select taskid,taskname,entrydt from task where task.entrydt >= @x i get following error"The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."Cheers |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-07-14 : 01:10:26
|
You have to use the format dd-mmm-yyyy or yyyymmdddeclare @x datetimeset @x= '29-Jun-2004'--orset @x= '20040629'select taskid,taskname,entrydt from task where task.entrydt >= @x MadhivananFailing to plan is Planning to fail |
 |
|
|
AndyB13
Aged Yak Warrior
583 Posts |
Posted - 2005-07-14 : 01:52:41
|
OrSET DATEFORMAT dmydeclare @x datetimeset @x= '29/06/2004' AndyBeauty is in the eyes of the beerholder |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2005-07-14 : 03:20:45
|
| I always try to use ISO format CCYYMMDD for representing dates as there is less chance for confusion - is 1/2/03 the First of Feb or the second of Jan?steveAlright Brain, you don't like me, and I don't like you. But lets just do this, and I can get back to killing you with beer. |
 |
|
|
|
|
|
|
|