Using MySQL
ID Date
001 2010-08-01
002 2010-08-15
003 2010-08-22
...
....
Query
select ID, Date from table where date < curdate + 7;
The above query is not working, it showing error.
How to get date upto nextweek date, I don't want to mentioned the date, it should calculate the systemdate + 7 days.
For Example
Today is 2010-06-30,
So it should take a value upto 2010-07-06
How to make a query for this condition....?
Using the DATE_ADD() function:
... WHERE date < DATE_ADD(CURDATE(), INTERVAL 7 DAY);
using an operator:
.... WHERE date < CURDATE() + INTERVAL 7 DAY
reference on date_add
I'm assuming that by curdate, you mean the function and not a column name. If it's a column name, change accordingly (although I wouldn't name a column after an existing mySQL function.)
Please try this
select ID, Date from table where Date < DATE_ADD(CURDATE(), INTERVAL 7 DAY)
Related
I am trying to extract x-days before and after a given date range. I have formulated the query as follows
SELECT a,b,date FROM table.test
WHERE date < "2012-09-07" - INTERVAL 2 DAY
AND date > "2012-09-08" + INTERVAL 2
The other approach which was NOT BETWEEN method also. None of them seems to be giving me right answer. The date is of type varchar(30) DEFAULT NULL within the database
Sample Data
a b date
2 4 2012-09-07
3 2 2012-09-05
5 3 2012-09-08
7 4 2012-09-07
8 5 2012-09-06
9 6 2012-09-07
3 7 2012-09-09
What I am looking for it the following:
a b date
3 2 2012-09-05
3 7 2012-09-09
The date is of type varchar(30) DEFAULT NULL within the database
Go for this:
SELECT a,b,date
FROM table.test
WHERE date < str_to_date('2012-09-07', '%Y-%m-%d') - INTERVAL 2 DAY
OR date > str_to_date('2012-09-08', '%Y-%m-%d') + INTERVAL 2 DAY;
Mainly, the AND needs to replaced by OR. I'd prefer an expicit (date) type conversion as well.
Update: re-reading your question carefully, the statement above might not be the one you are looking for. If Gordon go it right and if you want to use BETWEEN you might go for:
SELECT a,b,date
FROM table.test
WHERE date BETWEEN str_to_date('2012-09-07', '%Y-%m-%d') - INTERVAL 2 DAY AND
str_to_date('2012-09-08', '%Y-%m-%d') + INTERVAL 2 DAY
AND date NOT BETWEEN str_to_date('2012-09-07', '%Y-%m-%d') AND
str_to_date('2012-09-08', '%Y-%m-%d');
This is basically the same.
You just have your comparisons backwards:
SELECT a, b, date
FROM table.test
WHERE (date >= '2012-09-07' - INTERVAL 2 DAY AND
date < '2012-09-07'
) OR
(date > '2012-09-08' AND
date <= '2012-09-08' + INTERVAL 2
);
I'm not sure if you want >/< or >=/<=.
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)
I'm trying to get all records which have a date_created within 2 hours ago. It's a unix timestamp which is created from the php function time(). Here is my current query:
SELECT id from gsapi_synsets where name = "Beyonce" and date_created BETWEEN UNIX_TIMESTAMP(date_created) and UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 2 hour))
Doesn't seem to be working though.
If none of the date_created values will be in the future, you can use this:
SELECT id
FROM gsapi_synsets
WHERE name = 'Beyonce'
AND date_created > UNIX_TIMESTAMP(NOW() - INTERVAL 2 HOUR);
If date_created values can be in the future, use this, which cuts off at the current
SELECT id
FROM gsapi_synsets
WHERE name = 'Beyonce'
AND date_created BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 2 HOUR) AND UNIX_TIMESTAMP()
Note that calling UNIX_TIMESTAMP without an argument returns the timestamp for "right now".
You're trying to get all row's between that row's datestamp and 2 hours from now, which won't work. Use this instead:
SELECT id from gsapi_synsets where name = "Beyonce" and date_created BETWEEN UNIX_TIMESTAMP(DATE_ADD(NOW(),INTERVAL -2 hour)) and UNIX_TIMESTAMP(NOW())
I have a table like so:
id | gallons_used | date
----------------------
1 2 157263300
2 5 157262000
...
I want to get a result set containing only records that took place on X day of the week (Monday or Tuesday or Wednesday, etc etc)
Use DAYNAME() in your WHERE clause
WHERE DAYNAME(FROM_UNIXTIME(`date`)) = 'Monday' <-- by day of the week
AND `date` < INTERVAL CURRENT_DATE - 7 DAY <-- within the last week
You can use DAYOFWEEK() as well but this is more readable.
Something that would check for Wednesday as an example given that your date column is a timestamp:
DAYOFWEEK(date) = 4
Reference to documentation: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_dayofweek
SQLFiddle: http://www.sqlfiddle.com/#!2/12762/3
I had a FROM_UNIXTIME() call around date as well but I don't think it is required(not 100% sure). http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
Since you're using a Unix timestamp, you can use the following:
SELECT id, UNIX_TIMESTAMP(`date`)
FROM ...
WHERE DAYNAME(FROM_UNIXTIME(`date`)) = 'Monday'
AND `date` > UNIX_TIMESTAMP() - 604800
See the demo
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)