Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:37:23+00:00 2026-06-15T14:37:23+00:00

There seem to be a weird behaviour when comparing a date in table column

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:37:25+00:00Added an answer on June 15, 2026 at 2:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I want to do may seem like a weird scenario. Please keep in
There seem to be a number of weird things one could do if one
There seem to be very different opinions about using transactions for reading from a
There seem to be a limitation in the maximum number of clickable options in
There seem to be no property for an image to use at all, even
There seem to be many questions asked about this subject here on stackoverflow, but
There seem to be a .CFI directive after every line and also there are
There seem to be a number of different ways in which one can create
When designing an distributed application in Java there seem to be a few technologies
I have been writing Selenium test for web application and there seem to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.