July 13, 2005 at 5:11 pm
Hi friends
whats wrong with following query it returns incorrect results
declare @x varchar(15)
set @x= '''03/11/2003'''
select * from task where convert(varchar(10),task.entrydt,103) >= @x
it works only if i do
select * from task where convert(varchar(10),task.entrydt,103) = @x
but i want to be able to do >,>=, <= on dates
Thanks for any ideas
Cheers
July 13, 2005 at 5:18 pm
Too many tics' and you;ll want to declare @x as smalldatetime
declare @x SmallDatetime
set @x= '03/11/2003'
Which will evaluate to 03/11/2003 00:00:000
select * from task where task.entrydt >= @x
Can you post a create table definition and some sample data if your still having problems.
July 13, 2005 at 5:30 pm
Hi Ray
thanks for quick response.The reason i have ticks ' is am creating a dynamic sql from front end apps.
anyway i tried ur suggestion and unfortunatly did not work.
some of my data
taskid, taskname, entrydt
1, tong's first project, 2005-05-17 11:44:46.000
10, new task, 2003-12-24 16:17:59.000
1000, Inactive providers are di, 2003-11-03 00:00:00.000
1001, After selecting Claim or , 2003-11-03 00:00:00.000
1002, While changing the provid, 2003-11-03 00:00:00.000
1003, Visual acuity’s that are , 2003-11-03 00:00:00.000
1004, A few sites are occassion, 2003-11-03 00:00:00.000
1005, Stoping Letters.TextData , 2003-11-03 00:00:00.000
1007, Need a Ctrl-l for is high, 2003-11-03 00:00:00.000
1008, the arc32 form that we h, 2003-11-03 00:00:00.000
1009, Unable to create new ACC , 2003-11-04 00:00:00.000
1011, G.S: Friday 03-Oct-2003 c, 2003-11-05 00:00:00.000
1012, Ctrl-F7 . Default stateme, 2003-11-05 00:00:00.000
1013, IMM RNZGP Report : after , 2003-11-04 00:00:00.000
1014, , 2003-11-06 00:00:00.000
1015, When the tick is removed , 2003-11-06 00:00:00.000
i ran following i got abv data
declare @x datetime
set @x= '06/11/2003'
select top 16 taskid,taskname,entrydt from task where task.entrydt >= @x
July 13, 2005 at 5:46 pm
Well I guess I'm not sure whats not working,
Is EntryDT a datetime in the table?
Returns top 16 records where entryDT is newer or equal to @x.
Looks Ok to me.
July 13, 2005 at 7:19 pm
Yes Ray, entrydt is datetime field.
i have 2 records with following dates
2004-06-29 16:32:08.000
2004-06-29 12:37:18.000
when i run following query
declare @x datetime
set @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."
July 13, 2005 at 7:34 pm
declare @x datetime
set @x= '29/06/2004' --datetime doesn't like this format. How about '29-Jun-2004' ??
OR
'29/Jun/2004' ?
July 13, 2005 at 7:39 pm
That works Ray.thanks for that
but guess what if i change query to
declare @x datetime
set @x= '29/june/2004'
select taskid,taskname,entrydt from task where task.entrydt = @x
it does not return any results.
probably it is looking at time portion .that's why i was trying to convert it to a string and removing time part.
July 13, 2005 at 7:52 pm
one way to get around it...
select taskid,taskname,entrydt from task
where task.entrydt >= @x and task.entrydt < dateadd(dd,1,@x)
July 13, 2005 at 8:03 pm
Thanks ND.
that works fine. actually am looking for some general code
as where condition can contain any of the following filters
>,<,>=,<=
any ideas please.
Thanks
July 13, 2005 at 8:22 pm
Most of the times I go direct to books online.
July 13, 2005 at 11:19 pm
Ok, I'll skip past all the obvious arguments against using dynamic SQL
If you use the date format 'dd mmm yyyy' you should find your dates will convert correctly. Actually when you use the three character month, you can put the date parts in any order,
select '12 Dec 2004', CAST('12 Dec 2004' as datetime) select 'Dec 12 2004', CAST('Dec 12 2004' as datetime) select '2004 12 Dec', CAST('2004 12 Dec' as datetime) select 'Dec 2004 12', CAST('Dec 2004 12' as datetime)
--------------------
Colt 45 - the original point and click interface
July 14, 2005 at 10:32 pm
Use the ISO date format and it will not matter what DATEFORMAT your server or client uses:
declare @x varchar(8)
SET @x ='20030311'
select * from task where convert(varchar(8),task.entrydt,112) >= @x
select * from task where convert(varchar(8),task.entrydt,112) = @x
Andy
July 14, 2005 at 10:47 pm
Andy
I've 6 records with following date
2003-11-03 00:00:00.000
when i run ur second query it returns only 1 record!! i mean
declare @x varchar(8)
SET @x ='20030311'
select taskid,taskname,entrydt from task
where convert(varchar(8),task.entrydt,112) = @x
when i remove WHERE condition i get to see everything ,ofcourse
July 14, 2005 at 11:13 pm
And what does this return?
declare @x varchar(12) SET @x ='11 Mar 2003'select taskid,taskname,entrydt from task where entrydt = cast(@x as datetime)
--------------------
Colt 45 - the original point and click interface
July 15, 2005 at 3:18 am
Be sure about the datetime format, even if you read in a string, you should (pre) agree, the valid date(s) format(s).
If it differs from the host default datetime format, do the necessary convert(datetime,...) before you assign the value to @x (smalldatime)
But prefer @x as smalldatime because you may increase your chances of the proper index use. Otherwise, you may find yourself table scanning.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply