4

I would like to rows that have only been entered in the last 1 day.

I have a date column which stores YYYY-MM-DD, and I allow the user to send a date that they want to look at in this format yyymmdd how can I use this data to limit it to the previous day only?

I would imagine it is something to do with the BETWEEN keyword but I cant figure it out.

1
  • previous day = yesterday or (yyyymmdd - one day)? Commented Mar 22, 2011 at 15:42

4 Answers 4

10
SELECT * from TABLE_NAME WHERE ROW_DATE BETWEEN '2011-03-20' AND '2011-03-21'
Sign up to request clarification or add additional context in comments.

1 Comment

This will return records for 2011-03-21 00:00:00 as well.
7

This query:

SELECT  *
FROM    mytable
WHERE   mydate >= STR_TO_DATE('110321', '%y%m%d') - INTERVAL 1 DAY
        AND mydate < STR_TO_DATE('110321', '%y%m%d')

will return all records for Mar 20, 2011

3 Comments

this works for me if I used the string '110321' however it doesn't if I use "STR_TO_DATE('$testDate', '%y%m%d')" it doesn't $testDate = $_POST['date']; $testDate = (string)$testDate); The format of $testDate = YYYYMMDD
@Herly: use %Y%m%d (note the uppercase Y) for four-digit years.
Nevermind it WAS because i was using YYYMMDD instead of YYMMDD. thanks for your help.
1

From the MySQL manual (here):

SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= date_col;

Comments

1
SELECT * FROM YourTable WHERE date_column = DATE_ADD(CURDATE(), INTERVAL -1 DAY) 

This returns all rows for today and yesterday.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.