bonCodigo
bonCodigo

Reputation: 14361

Table Date column comparison with a generated dates list - MYSQL

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:

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.

Upvotes: 2

Views: 421

Answers (1)

fancyPants
fancyPants

Reputation: 51908

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.

Upvotes: 1

Related Questions