I would like to get a date range between LastYear/1/1 until LastYear/12/31
I know I could do this
date_sub(now(), interval 1 year). But this would get me 2013/03/08. Not sure how to change the day and the month.
SELECT *
FROM orders
WHERE dispatch_date between `LastYear/1/1` AND `LastYear/12/31`
You can easy to create the required dates:
SELECT *
FROM orders
WHERE dispatch_date >= MAKEDATE(YEAR(NOW()) - 1, 1) -- first day of previous year
AND dispatch_date < MAKEDATE(YEAR(NOW()), 1) -- first day of current year
I would suggest you to use YEAR().
SET #LastYear = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR));
SELECT *
FROM orders
WHERE dispatch_date
BETWEEN CONCAT(#LastYear,'-01-01') AND CONCAT(#LastYear,'-12-31')
Related
I have the query where i am getting the data but one year back till now,
select * from tblorders
where CreatedDateTime >= DATE_SUB(NOW(),INTERVAL 1 YEAR);
how can i get the data from the start of the current year till date data
start of the current year like 01/01/2021
select * from tblorders
where ( CreatedDateTime between DATE_FORMAT(NOW() ,'%Y-01-01') AND NOW() )
This would return from the beginning of the current year till current date
How about
select * from tblorders
where YEAR(CreatedDateTime) = YEAR(NOW());
DBFiddle: https://www.db-fiddle.com/f/d7h2raMmvxngn1uigPjoB5/0
I am trying to figure out how to select results based on the current date. The script will be ran daily but I am unsure of how to do it automatically.
SELECT * FROM Orders WHERE OrderDate='2008-11-11'
What I am trying now
SELECT * FROM Orders WHERE OrderDate=NOW() - INTERVAL 1 DAY
SELECT *
FROM Orders
WHERE OrderDate >= DATE(NOW())
AND OrderDate < DATE(NOW()) + INTERVAL 1 DAY
EDIT
I always tend to write the above style of query when I expect the column that I am checking against to have a time component as well.
If your OrderDate column does not have a time component, then as Lennart pointed out, you can simply do:
SELECT *
FROM Orders
WHERE OrderDate = DATE(NOW())
EDIT 2: Mihai's comment on your question is also relevant. You can simply use CURDATE() instead of DATE(NOW()).
You could do
SELECT * FROM Orders WHERE (OrderDate > DATE_SUB(NOW(),INTERVAL 1 DAY))
I'm trying to select all the data from the previous month to all the months in the future .. for example , I'd like to select everything from January till any date available in the future on the database, that goes for any month .. select the previous month till the future months of this year
This is my QUERY, It only starts with February , how can I make it start with the previous month .. current month - 1 is not working
SELECT *
FROM events
WHERE YEAR(event_start_date) = YEAR(CURDATE())
AND MONTH(event_start_date) = MONTH(CURDATE())
use DATE_SUB() to select previous month and >= to select all data in the future:
SELECT *
FROM events
WHERE YEAR(event_start_date) = YEAR(CURDATE())
AND MONTH(event_start_date) >= MONTH(DATE_SUB( CURDATE(), INTERVAL 1 MONTH))
Try:
AND MONTH(event_start_date) = MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTHS))
The portion DATE_SUB(CURDATE(), INTERVAL 1 MONTH) will subtract a month from the current date. If you want last month and everything in the future, use:
AND MONTH(event_start_date) >= MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTHS))
Notice '>='. Although there is an edge case at january that you'll have to get around. The best way might be like this:
WHERE event_start_date >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 1 MONTHS), (DAYOFMONTH(CURDATE)) DAYS)
Which will get you everything from the 1st of last month. No edge cases.
Try this
SELECT *
FROM events
WHERE YEAR(event_start_date) = YEAR(CURDATE())
AND MONTH(event_start_date) >= MONTH(CURDATE() - INTERVAL 1 MONTH);
SELECT *
FROM events
WHERE event_start_date >= '1/'+MONTH(DATE_SUB( CURDATE(), INTERVAL 1 MONTH))+'/'+YEAR(DATE_SUB( CURDATE(), INTERVAL 1 MONTH))
For performance, you'd likely want an index range scan operation on the event_start_date column. That means (obviously), you'd want an index with event_start_date as a leading column.
To get an index range scan, the predicate needs to be on the bare event_start_date column, and NOT a function.
WHERE event_start_date >= some_value
For "some_value" in this case, one possible expression you can use would be:
CAST(DATE_FORMAT(NOW()+INTERVAL -1 MONTH ,'%Y-%m-01') AS DATE)
That takes the current date and time, subtracts one month, and then sets the day and time component to midnight of the first of the month.
my datefield (artists.onlineDate) is yyy-mm-dd hh:mm:ss
right now I got:
-- Today
SELECT * FROM artists WHERE DATE(artists.onlineDate) = CURDATE()
-- Current Week
SELECT * FROM artists WHERE WEEK(artists.onlineDate, 3) = WEEK(CURDATE(), 3)
-- Current Month
SELECT * FROM artists WHERE MONTH(artists.onlineDate) = MONTH(CURDATE())
-- Current Year
SELECT * FROM artists WHERE YEAR(artists.onlineDate) = YEAR(CURDATE())
But what I need is exact: Yesterday, Last Week, Last Month, Last Year
I try to explain. if we got wednesday, and I use SELECT * FROM artists WHERE DATE(artists.onlineDate) = CURDATE(), then I get monday to wednesday.
I would like to have the monday to sunday of the last week. the calendar week before.
And the same for the month and year.
I thins SUB_DATE is not the right way.
Any suggestions?
OK I found what I was looking for at:
MySQL Query to select data from last week?
SELECT * FROM testwoche
WHERE WEEK (datum, 3) = WEEK(current_date, 3) - 1
AND YEAR(datum) = YEAR(current_date) ORDER BY datum DESC
same for month
SELECT * FROM testwoche
WHERE month (datum) = month(current_date) - 1
AND YEAR(datum) = YEAR(current_date)
ORDER BY datum DESC
This gives back the last week from monday to sunday and the last month
Thanks everybody for helping!
I think you can use the SUBTIME function: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_subtime
I haven't had time to really test it, but I think you get the idea:
Yesterday:
SELECT * FROM artists WHERE DATE(artists.onlineDate) = SUBTIME(CURDATE(),'1 0:0:0');
Last Week
SELECT * FROM artists WHERE WEEK(artists.onlineDate, 3) = WEEK(SUBTIME(CURDATE(),'7 0:0:0'), 3)
Last Month
SELECT * FROM artists WHERE MONTH(artists.onlineDate) = MONTH(SUBTIME(CURDATE(),'31 0:0:0'))
Last Year
SELECT * FROM artists WHERE YEAR(artists.onlineDate) = YEAR(SUBTIME(CURDATE(),'365 0:0:0'))
To get a specific week/month/year (which still works when your db contains several years worth of data and when your current time is the start of a new year), this should work:
SELECT * FROM testwoche WHERE
((YEAR(CURDATE())-YEAR(datum)))*52-WEEK(datum,3)+WEEK(CURDATE(),3) = 1;
If you want two weeks ago, you could change it to =2 (if you want current week, from Monday to current day: =0). If you want last month, you change the WEEK function to MONTH.
I have a table in my database with two columns: month (1-12) and year (yyyy).
I need to select records between two dates, for exemple
select * from Calendar a where SOMEDATE between STARTDATE and ENDDATE.
So the question is: how can I create the STARTDATE and the ENDDATE from this two columns I have?
...where SOMEDATE between
STR_TO_DATE(CONCAT_WS('-',STARTYEAR,STARTMONTH,1),'%Y-%m-%d')
and
DATE_SUB(
STR_TO_DATE(CONCAT_WS('-',ENDYEAR,ENDMONTH + 1,1),'%Y-%m-%d')
, INTERVAL DAY 1
)
Note that we convert both parts to type date, and use date_sub to subtract a single day from ENDMONTH + 1, since we don't know how many days there are in the relevant month.
You can use this solution to make date from the year and month fields-
SELECT MAKEDATE(year, 1) + INTERVAL month - 1 MONTH FROM calendar;
The apply this one to WHERE condition, e.g. -
SELECT * FROM Calendar a
WHERE
MAKEDATE(year, 1) + INTERVAL month - 1 MONTH BETWEEN
#STARDATE - INTERVAL EXTRACT(DAY FROM #STARDATE) - 1 DAY
AND
#ENDDATE
But I have to ask you about the STARTDATE and ENDDATE criterias. What to do if STARTDATE is '2012-09-20'? Should the query return records with month = 9?