Sample Header Ad - 728x90

Sybase IQ cannot select by a calculated date

1 vote
0 answers
441 views
Question: --------- I have a query to select rows dated "last Friday from a given date", using the dateadd/datepart/modulo 7 trick. It works fine in Sybase ASE, but returns 0 rows in Sybase IQ. If I hard code the date into the query, it works in IQ as well, but I need the query to be dynamic. __Sample data:__ create table #tmp1 (test_date date) insert into #tmp1 values ('2017-09-08') -- Sep 08 is a Friday __Expanded query that works in ASE but not in IQ__ Note the date provided is Tue Sep 12, and it finds "last Friday" from that date, which should be Sep 08. select * from #tmp1 where test_date = convert( date, dateadd(day, -((datepart(cdw, '2017-09-12') + 2) % 7), '2017-09-12') ) The gist of the query is that it will take the given date (Tue Sep 12) and find out how many days it has been since Friday. It then subtracts that many days from the given date, and uses that date in the where clause. I expect the query to return the row in the temp table, and it works in ASE, but returns 0 rows in IQ. Can anybody explain why? If I hard code the Friday date, it finds the row just fine. __Edit:__ I've found that assigning the "last Friday" date to a variable, and then querying with that variable _does_ work. Details below. However, using a variable isn't an option in this case, and I'm still very confused about why "placing the calculated date into a variable, then using the variable" works, but "calculating the date directly in the where clause" does not. --- Some debugging samples when querying #tmp1 in IQ: ------------ __Actual query that fails in IQ (it returns the row correctly in ASE):__ select * from #tmp1 where test_date = convert(date, dateadd(day, -((datepart(cdw, '2017-09-12') + 2) % 7), '2017-09-12')) >0 rows __Hardcoded date:__ select * from #tmp1 where test_date='2017-09-08' >9/8/2017 --- Deconstructing the query, to make sure the values are correct: ------------ __How many days are subtracted via the dateadd:__ select ((datepart(cdw, '2017-09-12') + 2) % 7) >4 __What date is calculated by the dateadd:__ select dateadd(day, -((datepart(cdw, '2017-09-12') + 2) % 7), '2017-09-12') >9/8/2017 12:00:00 AM __Nothing odd happens when it gets converted to date:__ select convert(date, dateadd(day, -((datepart(cdw, '2017-09-12') + 2) % 7), '2017-09-12')) >9/8/2017 --- Using a variable ---------------- I've found that assigning the calculated date to a variable first _does_ allow the query to work. However, variables aren't an option in my case (and really, this just raises more questions for me - why does it work when the value is stored in a variable, but not when place directly in the query): __Select via @val__ declare @val date select @val = convert(date, dateadd(day, -((datepart(cdw, '2017-09-12') + 2) % 7), '2017-09-12')) select * from #tmp1 where test_date = @val >9/8/2017 __Value of @val:__ select @val >9/8/2017
Asked by RToyo (167 rep)
Oct 13, 2017, 03:43 PM
Last activity: Oct 16, 2017, 03:27 PM