2

There seem to be a weird behaviour when comparing a date in table column with a date generated in a list within MYSQL.

Please take a look at the * SQLFIDDLE reference.

Payroll Table:

ID  DATESTAMP
1   August, 30 2012 00:00:00+0000
2   September, 02 2012 00:00:00+0000
3   September, 15 2012 00:00:00+0000
4   September, 24 2012 00:00:00+0000
5   October, 05 2012 00:00:00+0000
6   October, 16 2012 00:00:00+0000
7   October, 19 2012 00:00:00+0000
8   November, 02 2012 00:00:00+0000
9   November, 10 2012 00:00:00+0000
10  November, 16 2012 00:00:00+0000
11  November, 24 2012 00:00:00+0000
12  November, 30 2012 00:00:00+0000
13  December, 01 2012 00:00:00+0000
14  December, 07 2012 00:00:00+0000

Dates list is generated between two particular dates with a constant day interval

Query:

set @i:= 0;

SELECT date_format(DATE(ADDDATE('2012-10-05', 
INTERVAL @i:=@i+14 DAY)),'%Y-%m-%d')
AS dateP, @i
FROM payroll
HAVING @i < datediff(now(), date '2012-10-05')
;

DATEP          @IntervalDays
2012-10-19     14
2012-11-02     28
2012-11-16     42
2012-11-30     56
2012-12-14     70

As you can see the generated dates list has matches to the Payroll table above. However when the comparison is done, it reutns zero records.

Comparison Query:

set @i:= 0;

SELECT distinct datestamp FROM payroll
WHERE date(datestamp) in (
SELECT DATE(ADDDATE('2012-10-05', 
INTERVAL @i:=@i+14 DAY) ) AS dateP
FROM payroll
where @i < DATEDIFF(now(), date '2012-10-05') 
)
;

So Questions I have:

  • Is the inner query stop generating dates when used as a nested query?

  • Is there anything wrong with the dates comparison method I am using here?

  • What could be the reason for this entire failure?

  • How to fix it within Select itself without any procedure/functions? :)

PS:

I am also trying to test this in SQL server as well as Oracle.

There are many good questions and answers to support 'Date Comparison' issues occurred at various scenarios with the site. That includes posts such as mysql date comparison with date_format. etc.. May be there's one hidden somewhere asking for exact issue I am facing with different wording. Couldn't find and hence posted the question.

Community
  • 1
  • 1
bonCodigo
  • 14,268
  • 1
  • 48
  • 91

1 Answers1

1

Second UPDATE:

Now I got it working in every version:

select
*
from
Payroll
inner join 
(
SELECT DATE(DATE_ADD('2012-10-05', 
INTERVAL @i:=@i+14 DAY) ) AS dateP
FROM Payroll, (SELECT @i:=0) r
where @i < DATEDIFF(now(), date '2012-10-05') 
) sq on Payroll.datestamp = sq.dateP

You just have to initialize the variable inside the query.

UPDATE:

Strange thing is, this one works on my local machine without problems (version 5.1.41-3ubuntu12.7-log), but not in your SQLfiddle.

set @i:= 0;
select
*
from
Payroll
inner join 
(
SELECT DATE(DATE_ADD('2012-10-05', 
INTERVAL @i:=@i+14 DAY) ) AS dateP
FROM Payroll
where @i < DATEDIFF(now(), date '2012-10-05') 
) sq on Payroll.datestamp = sq.dateP

END OF UPDATE

Have you tried it like this?

set @i:= 0;

SELECT distinct datestamp FROM payroll
WHERE STR_TO_DATE(datestamp, '%M, %d %Y %H:%i:%f') in (
SELECT DATE(ADDDATE('2012-10-05', 
INTERVAL @i:=@i+14 DAY) ) AS dateP
FROM payroll
where @i < DATEDIFF(now(), date '2012-10-05') 
)
;

My guess is, that the DATE() function fails, cause you're varchar(is it?) date is not in ISO format. Therefore you have to use STR_TO_DATE() function.

For exact usage of STR_TO_DATE() read here and here. I'm not sure about the microsecond part.

fancyPants
  • 50,732
  • 33
  • 89
  • 96
  • @bonCodigo It helps both you AND me, if you give some feedback. – fancyPants Dec 09 '12 at 10:55
  • thank you for your answer and taking time to look into this. I have already tried out almost each possible relevant date function I could use to get the dates out. Including your, nothing has worked so far. I will update the question with some finding I happened to notice. E.g. if you do a simple `SELECT * FROM` above dates list query, it still doesn't return anything at all. Glad to hear if you got any other magic. – bonCodigo Dec 10 '12 at 10:09
  • thanks for working on this mate. Can you please give me your MYSQL SQLFiddle link for your updated answer as well? Cheers. – bonCodigo Dec 11 '12 at 06:09
  • @bonCodigo I just used yours http://sqlfiddle.com/#!2/1e031/34 and pasted my query into it :) – fancyPants Dec 11 '12 at 07:53
  • Your query surely works @tombom, I had no much time to check on Oracle and SQL Server for the same. But very much interested in checking though. You can give it a try on those two as well. But for now, yours is the answer ;) thanks. – bonCodigo Dec 11 '12 at 08:04