For getting last 8 days details I am using this condition in my query.
bill_date <= ( CURDATE( ) - INTERVAL 8 DAY )
But I am not getting the proper result.Last 8 days means 25th april - 2nd may. What is wrong in my condition and is there any another way to do the same?
Try this method
bill_date >=DATE_ADD(CURENT_DATE(),INTERVAL -8 DAY ) AND bill_date<CURRENT_DATE()
Try this
bill_date >= (sysdate - 8)
Try this to get last 8 days details
bill_date >= ( CURDATE( ) - INTERVAL 8 DAY ) --After 25th Apr
or (if you have records in future date, then use this query)
bill_date >= ( CURDATE( ) - INTERVAL 8 DAY ) AND bill_date <= CURRENT_DATE() -- From 25th Apr - 2nd May
it should be bill_date >= ( instead of <= ), because you want the result after the 25th Apr. So your condition should be bill_date >= 25th Apr.
Try this
bill_date between CURDATE() - INTERVAL 8 DAY and curdate() + interval 1 day - interval 1 second
Related
I'm trying to select all rows that have a date greater than or equal to the previous Sunday at 8 PM. So, if it were 7 PM on a Sunday right now, the selection would contain almost a full week of data. And if it were 9 PM on a Sunday right now, the selection would contain about an hour's worth of data.
So the following query isn't what I'm interested in, because it doesn't start and end on Sunday at 8 PM.
SELECT * FROM table WHERE date >= DATE_SUB(NOW(), INTERVAL 1 WEEK)
I'm not sure how to do write the correct query though.
You can use the cast statement to tweak the logic when your criteria matches.
Change the time Interval accordingly. I didn't added seconds so both the conditions will take 8 PM data.
SELECT * FROM table
WHERE date >= select CASE WHEN (DAYOFWEEK(NOW()) = 1 AND CURTIME() >= '20:00:00') THEN DATE(NOW()) + INTERVAL 20 HOUR
WHEN (DAYOFWEEK(NOW()) = 1 AND CURTIME() <= '20:00:00') THEN DATE_ADD(DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())+6 DAY), interval 20 hour)
ELSE DATE_ADD(DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 DAY), interval 20 hour) END
I am trying this query
SELECT *
FROM flexible_products
WHERE YEAR(updated_at) = YEAR(DATE_SUB(CURDATE(), INTERVAL -1 MONTH))
AND MONTH(updated_at) = MONTH(DATE_SUB(CURDATE(), INTERVAL -1 MONTH))
for my current work. What I want to do is to get all rows from the previous month,
Like for example, we have today July 04, 2017, I would like to get rows from January to June, which is not the outcome of the above query.
What is the best way to this in MySQL?
The following query will return all records updated on or after January 1 of the current year but stictly before the first of the current month.
SELECT *
FROM flexible_products
WHERE updated_at >= DATE_FORMAT(NOW(), '%Y') AND
updated_at < DATE_FORMAT(NOW() ,'%Y-%m-01')
Follow the link below for a running demo:
Rextester
You should also try this or you can get data between any two dates.
SELECT * FROM flexible_products WHERE updated_at BETWEEN DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-01'), INTERVAL -3 MONTH) AND NOW( )
date output is:-
DATE_ADD(DATE_FORMAT(NOW(),'%Y-%m-01'), INTERVAL -3 MONTH) //2017-04-01
NOW( ) //2017-07-04 11:17:38
a bit shorter. Not sure if also faster
SELECT *
FROM flexible_products
WHERE
YEAR(updated_at) = YEAR(CURRENT_DATE) AND
MONTH(updated_at) < MONTH(CURRENT_DATE())
If i understand you correctly you can do this
WHERE updated_at <= '2017-07-01' AND updated_at >= '2017-01-01'
I am trying to retrieve all data from table in a specific month, my date format in the database table is 2016-06-19 I am need to find with the respect of a selected date, like if I select 2016-05-22
it will retrieve 2016-05-07 to 2016-06-06 interval values from the table.
And also to all the values in this month which is 05 month, I am using mysqli with PHP. I have tried using this query
SELECT * FROM t_tenancy_details WHERE
agreement_date >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )AND
agreement_date < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )
This works fine for the running month, But problem is that if I select a previous month it does not work.
It's not very clear from the question as to what is needed. Based on the 2 requirements that I could understand:
Get everything surrounding the given date (i.e. +-15 days from the given date):
SELECT *
FROM t_tenancy_details
WHERE agreement_date >= DATE_SUB(#d, INTERVAL 15 DAY)
AND agreement_date <= DATE_ADD(#d, INTERVAL 15 DAY);
Get all the records where the month is same as the month in the given date:
SELECT *
FROM t_tenancy_details
WHERE MONTH(agreement_date) = MONTH(#d)
AND YEAR(agreement_date) = YEAR(#d);
where #d is the input date, in your case - CURRENT_DATE
Should be this
SELECT * FROM t_tenancy_details
WHERE (YEAR(t_tenancy_details) => YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(agreement_date) => MONTH(CURRENT_DATE - INTERVAL 1 MONTH))
AND (YEAR(t_tenancy_details) <= YEAR(CURRENT_DATE)
AND MONTH(agreement_date) <= MONTH(CURRENT_DATE))
I m trying to get last 2 and 3 months data.
e.g Last 2 months data means Dec 2014 To Jan 2015 and 3 months means Nov 2014 TO Jan 2015
I tried
SELECT * FROM d_jobs WHERE `job_date` >= DATE_ADD( NOW( ) , INTERVAL -1 MONTH )
Here Its Count last 30 days data only .
So Which is the best way to perform this ?
Try this:
SELECT * FROM d_jobs WHERE `job_date` >= last_day(now()) + interval 1 day - interval 3 month;
Try with this query hope you will overcome your problem i think
select * from d_jobs where job_date < Now() and job_date > DATE_ADD(Now(), INTERVAL- 3 MONTH);
select * from d_jobs where job_date BETWEEN SUBDATE(CURDATE(), INTERVAL 3 MONTH) AND NOW();
select * from d_jobs WHERE job_date BETWEEN STR_TO_DATE('2012-09-01', '%Y-%m-%d') AND NOW();
Get left 7 chars of current date in ISO format (i.e. '2015-01'). Then add "-01" for 1st day of month (result is "2015-01-01"). Then use INTERVAL ... MONTH to get last months.
Hey I'm a bit of a beginner with SQL but i'm trying to write a query to pull out all records if its someones birthday within the next 7 days and have looked at other threads with answers but I'm having trouble adapting them to my setup.
SELECT *
FROM `QG04c`
WHERE month( `dob` ) = month( now( ) )
AND day( `dob` )
BETWEEN day( now( ) )
AND day( now( ) ) +7
AND `Primary Unit?` =1
At the moment this pulls out everyone whose birthday is within 7 days but I don't think it will cope with end of the month scenarios and end of year etc.
Assuming this is MySQL, you can use the DAYOFYEAR function to get the day of the year a birthday occurs. The simple scenario is everything from 1st Jan - 25th December, where 7 days ahead is in the same year. For this you can use:
SELECT *
FROM `QG04c`
WHERE DAYOFYEAR(DOB) - DAYOFYEAR(CURDATE()) BETWEEN 0 AND 7;
However when today is between 25th Dec - 31st Dec you need to account for the birthday being between 1st and 6th Jan. To do this you need:
SELECT *
FROM `QG04c`
WHERE DAYOFYEAR(CURDATE() + INTERVAL 7 DAY) < 7
AND DAYOFYEAR(DOB) < DAYOFYEAR(CURDATE() + INTERVAL 7 DAY);
Then it is just a matter of combining the two cases:
SELECT *
FROM `QG04c`
WHERE DAYOFYEAR(DOB) - DAYOFYEAR(CURDATE()) BETWEEN 0 AND 7
OR ( DAYOFYEAR(CURDATE() + INTERVAL 7 DAY) < 7
AND DAYOFYEAR(DOB) < DAYOFYEAR(CURDATE() + INTERVAL 7 DAY)
);
Examples on SQL Fiddle
select * from tablename WHERE [ColumnName] BETWEEN DATEADD(DAY, +7, #YourDate) AND #YourDate and Primary Unit = 1 (If you have and condition put add here)
Thanks .
Should be easy if you don't decompose the day?
WHERE `dob` BETWEEN now() AND now() +7