I have a date field in the database table of this format 2012-02-1.i need to write 3 different queries:
a.) I need to retrieve all fields where date is between today and previous 5 days.
b.) I need to retrieve all fields where date is older than 5 days from today's date.
c.) I need to retrieve all fields where date between '5 days ago' to '30 days ago'
Can I use some inbuilt mysql function.
Manipulating the query below:
SELECT fields
FROM table
WHERE date >= CURDATE() - 5
or something like this
Or using a between clause. I am not getting the syntax correct.
SELECT p.status,p.downpayment_date,p.policy_id,i.id,i.policy_type,i.carrier,i.policy_number,i.client_id,c.id,c.client_name FROM pdp_payment AS p,pdp_policy_info AS i,pdp_client_info AS c WHERE p.policy_id=i.id AND i.client_id=c.id AND (((p.status='close pending') OR (p.status='Cancel')) AND (p.downpayment_date BETWEEN ((INTERVAL 5 DAY AND CURDATE()) - (INTERVAL 30 DAY AND CURDATE()))) )
Date between today and previous 5 days.
SELECT fields FROM table
WHERE date_field BETWEEN CURRENT_DATE - INTERVAL 5 DAY AND CURRENT_DATE
Date smaller than previous 5 days.
SELECT fields FROM table
WHERE date_field < CURRENT_DATE - INTERVAL 5 DAY
For all fields where date is between today and previous 5 days.
SELECT fields
FROM table
WHERE your_date_field_name BETWEEN CURDATE() - INTERVAL 5 DAY AND CURDATE()
You can work out other problems in a similar way
date is a keyword, so when you use it as a field name it MUST be enclosed in backticks ` otherwise you will get a parse error.
To get the range you want:
WHERE `date` BETWEEN DATE_ADD(NOW(),INTERVAL -30 DAY) AND DATE_ADD(NOW(),INTERVAL -5 DAY)
Related
I'm trying to get all rows from DATE column
values from 10 days ago till today
i'm trying to undesrtand why this syntax isn't working:
select * from table WHERE date BETWEEN NOW() AND NOW() - INTERVAL 10 DAY ORDER BY date
You have to start with the lower value when using between
select *
from table
WHERE date BETWEEN NOW() - INTERVAL 10 DAY and NOW()
ORDER BY date
The problem is not related to the NOW() function but to the BETWEEN operator, the lower timestamp has to be specified first:
where date between now() - interval 10 day and now()
however, depending on your requirements, you might want to use this:
where date between current_date() - interval 10 day and current_date()
or just
where date>=current_date() - interval 10 day
now() returns a timestamp that contains date and time information, while current_date() returns just the current date without time information. If date is just a date column, without time information, using now() - interval 10 day you will get just the latest 9 days and not the latest 10 as you might expect.
I've a table in a db with some date field with format yyyy-mm-dd
I'm trying to perform a query that take just records with a interval of 3 month from today.
I've done like this
WHERE DATE_SUB(myTable.myField, INTERVAL 3 MONTH) = CURDATE()
and it works, but my second step is ignore years of my date field and from curdate().
I've tried EXTRACT or DATEFORMAT, but query doesn't work with those function.
How can I modify my query?
Thanks
The condition is wrong.
Try this instead:
...WHERE myTable.myField >= CURDATE() - INTERVAL 3 MONTH...
EDIT:
Based on your comment:
with my query i've got all record that have in dateField this date
'2016-12-07' (curdate() is today '2016-09-07') and it's fine. but i
want that query gives me also date that have 12 on month and 07 on
day, ignoring year. Eg. if i have '2016-12-07' and '2014-12-07', my
query must give me both records. it's a query that will run every day
...WHERE DATE_FORMAT(myTable.myField,'%m-%d') =
DATE_FORMAT((CURDATE() + INTERVAL 3 MONTH),'%m-%d')...
Use below condition which will find records with a interval of 3 month from today.
WHERE myTable.myField = DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
Why this query is not working
SELECT * FROM history WHERE DATE(date) < CURDATE() + 30
I am trying to get the data from 30 days but my query is not working.Why
What does +30 mean? Days? Years? Months? Hours? You need to use (the proper syntax) a format MySQL understands:
SELECT * FROM history WHERE DATE(date) < CURDATE() + INTERVAL 30 DAY
To get the data from today on to 30 days after current day, you've got to set an upper and an lower limit, so use:
SELECT * FROM history WHERE
date >= CURDATE()
AND
date < CURDATE() + INTERVAL 31 DAY
Please note that by not using a function on your date column you won't prohibit MySQL to use an index on this column.
The lower limit should be obvious, the upper limit means that you've got the complete day that's 30 days later than today. If you use + INTERVAL 30 DAY instead this last day is excluded from the result.
Because you're not using the right construct, try:
SELECT * FROM history WHERE DATE_ADD(date, INTERVAL 30 DAY);
I need to check for entries made in the last "x" days (example 30 days) and cannot get the query to work. This is what I am using:
SELECT CAL_OWNER,
CAL_TITLE,
FROM_UNIXTIME (CAL_CREATED, "%m-%d-%y") AS CREATED,
FROM_UNIXTIME (RANGE_START, "%Y-%m-%d") AS DATE2BESEEN,
CASE CAL_REFERRAL_TYPE
WHEN 1 THEN 'NoReferral'
WHEN 2 THEN 'CareyGuide'
WHEN 3 THEN 'Education'
WHEN 4 THEN 'Employment'
WHEN 5 THEN 'Housing'
WHEN 6 THEN 'Medical'
ELSE 'NA'
END
AS REFERRALS
FROM EGW_CAL
WHERE CAL_CREATED BETWEEN (NOW () - '30 day') AND NOW ()
ORDER BY REFERRALS ASC;
If I comment out the "WHERE range_start ... line the query runs fine, but pulls all data
However, if I run the complete query, it does not error, but there are no results (I have 4 entries in column cal_created in the last 3 weeks).
If some one can help I'd really appreciate it
Try using INTERVAL and either NOW() or CURDATE()..
WHERE FROM_UNIXTIME (CAL_CREATED,'%Y-%m-%d') BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
curdate is just the date portion of the day
if you want to include the time use NOW()
WHERE FROM_UNIXTIME (CAL_CREATED,'%Y-%m-%d') BETWEEN NOW() - INTERVAL 30 DAY AND NOW()
you could also make a new date to use the between with
WHERE FROM_UNIXTIME (CAL_CREATED,'%Y-%m-%d') BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
SOURCE: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
NOTE: the dates need to be formatted correctly in order for it to work
FULL QUERY:
SELECT
CAL_OWNER,
CAL_TITLE,
FROM_UNIXTIME (CAL_CREATED, '%m-%d-%y') AS CREATED_AT,
FROM_UNIXTIME (RANGE_START, '%Y-%m-%d') AS DATE2BESEEN,
CASE CAL_REFERRAL_TYPE
WHEN 1 THEN 'NoReferral'
WHEN 2 THEN 'CareyGuide'
WHEN 3 THEN 'Education'
WHEN 4 THEN 'Employment'
WHEN 5 THEN 'Housing'
WHEN 6 THEN 'Medical'
ELSE 'NA'
END AS REFERRALS
FROM EGW_CAL
WHERE FROM_UNIXTIME(CAL_CREATED,'%Y-%m-%d') BETWEEN (NOW() - INTERVAL 30 DAY) AND NOW()
ORDER BY REFERRALS ASC;
CAL_CREATED is a UNIX timestamp,
NOW() will return a MySQL timestamp.
They don't mix automatically. So use
WHERE CAL_CREATED
BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 30 DAY) AND UNIX_TIMESTAMP(NOW());
Note:
I wouldn't recommend to go the other way
WHERE FROM_UNIXTIME(CAL_CREATED) BETWEEN ...
because MySQL can't use an index in this case.
The correct where clause uses internval:
WHERE CAL_CREATED BETWEEN NOW() - interval 30 day AND NOW()
The use of single quotes is reminiscent of Postgres. In MySQL, it ends up treating the value of now() as an integer. And it subtracts the string value "30 days" from it.
When now() is treated as an integer, it also has hours, minutes, and seconds. So you are really subtracting something like 30 seconds. Here is the documentation on now().
I'm using
SELECT * from tbl_name WHERE DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
to select data for specific days. The problem is that line gets data right before 3 days.
What to do so selected data to be period three days before till now ?
First your field should be of type datetime or date and then you can use a between clause
your_date_field BETWEEN now() - INTERVAL 72 HOURS AND now()