Getting Data from current Year till date - mysql

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

Related

SQL MAX datetime - 1 day

I want to do a comparison between two dates. The highest date (currently via MAX datetime) is working, but I can't get the day after the highest date to compare the data with.
I'm using the following to get the data of the highest available date:
SELECT `datetime`, `standardSubscriptionDuration`,
SUM(`activeStandardPriceSubscriptions`) AS OneMonthActiveStandard
FROM `Subscription_totals`
WHERE `standardSubscriptionDuration` = '1 Month'
AND `datetime` = (SELECT MAX(`datetime`) AS Date FROM `Subscription_totals`)";
I already tried:
(SELECT MAX(`datetime`) -1 AS Date
But this won't give the result. How am I able to get the data of yesterday and eventually compare them?
I think that you want the following date arithmetics:
WHERE
`standardSubscriptionDuration` = '1 Month'
AND `datetime` = (
SELECT MAX(`datetime`) - interval 1 day AS Date FROM `Subscription_totals`
)

SQL condition date

From this database, I want to select those data which payment_date is current month but the service_date is any month except this month.
Database sample
One way to check if 2 dates are in the same month is with the use of the function last_day(), because if for 2 dates the last day of their month is the same then they are in the same month:
select *
from tablename
where last_day(payment_date) = last_day(curdate())
and last_day(service_date) <> last_day(curdate())
If you want the results for any month and not for the current month only:
select *
from tablename
where last_day(payment_date) <> last_day(service_date)

MySQL to get previous year record

I want a MySQL query to fetch previous year records.
I already wrote a query to fetch current year records but I want previous year record also. There is a column called "date_created" based upon this date I have to fetch the status of the meterial.
SELECT material_status, COUNT(*) c
FROM purchase_order
WHERE YEAR(date_created) = YEAR(CURDATE()) AND material_status='open';
to get last year data
SELECT material_status, COUNT(*) c
FROM purchase_order
WHERE YEAR(date_created) = YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) AND material_status='open';
Query to see how the date time functions for MySQL works
Try This:
select
CURRENT_USER() user,
CURRENT_TIME() current_time,
CURRENT_DATE() current_date,
year(CURRENT_DATE()) current_year,
YEAR(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) previous_year;

How to get last year's date range?

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')

MySQL results yesterday, last week, last month and last year

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.