I'm trying to query my database to get all infrastructures with a due date (inf_lifespan) is between 6 months away from today's date.
inf_lifespan
2028-09-19
2025-12-04
2020-11-11
2026-02-17
2019-12-12
2020-11-13
2018-12-13
Running this query will generate no record but I expect that infrastructure with a due date of 2018-12-13 should be the outcome of the query.
select *
from tbl_infrastructure
where `inf_lifespan` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH ) AND DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH )
I m using 5.7.21 - MySQL Community Server (GPL)
Can anyone help me with this, please
In the condition BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH ) AND DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH )
you will the date from 6 months ago to today.
You can try to use DATE_ADD instead of DATE_SUB, because of DATE_SUB function is sub the month.
select *
from tbl_infrastructure
where `inf_lifespan` BETWEEN CURDATE() AND DATE_ADD( CURDATE(), INTERVAL 6 MONTH)
Related
I'm blocking on an SQL query. I am looking to recover the turnover of last year but the day before. For example: we are the 13/08/19 and I wish the turnover of yesterday of last year so 12/08/18.
How can I do that? Here is my request:
SELECT SUM(total_paid/1.2)
FROM '._DB_PREFIX_.'orders
WHERE o.date_add BETWEEN DATE_FORMAT(
CASE
WHEN YEAR( DATE_SUB( now(), INTERVAL 364 DAY ) ) = YEAR(now() )
THEN DATE_SUB(concat(YEAR(now()),'-',MONTH(now()),'-01'), INTERVAL 371 DAY)
ELSE DATE_SUB(concat(YEAR(now()),'-',MONTH(now()),'-01'), INTERVAL 364 DAY) END, '%Y/%m/%d') AND
CASE
WHEN YEAR( DATE_SUB( now(), INTERVAL 364 DAY ) ) = YEAR(now() )
THEN DATE_SUB(now(), INTERVAL 371 DAY)
ELSE DATE_SUB(now(), INTERVAL 364 DAY) END
AND valid=1
table
Thanks for help.
It seems you should be able to simplify your query to something like this:
SELECT SUM(total_paid/1.2)
FROM '._DB_PREFIX_.'orders
WHERE DATE(o.date_add) = CURDATE() - INTERVAL 1 YEAR - INTERVAL 1 DAY
Note that dependent on how you want to treat February 29, you may want to change the expression to
CURDATE() - INTERVAL 1 DAY - INTERVAL 1 YEAR
In the first form, 2020-02-29 will map to 2019-02-27, in the second it will map to 2019-02-28.
{
select sum(your_total_column/1.2) from your_tab
where now() =(CASE WHEN mod(year(your_date_column),4) = 0
THEN DATE_ADD(your_date_column,INTERVAL 367 DAY)
ELSE DATE_ADD(your_date_column,INTERVAL 366 DAY));}
The idea is find if your_date_column is leap year if it is leap year then add 367 else add 366 and compare with current date.
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
I am using date_add function to add 12 hours and 30 minutes in current date.
select DATE_ADD( CURDATE( ) , INTERVAL '12:30' HOUR_MINUTE )
gives me 2016-01-03 12:30:00
I just need date after adding 12:30.
what is the correct way to do same?
Just use the date function:
select DATE(DATE_ADD( CURDATE( ) , INTERVAL '12:30' HOUR_MINUTE ))
You can also express the logic using ADDTIME():
SELECT DATE(ADDTIME(currdate(), TIME('12:30:00')))
use NOW
SELECT DATE_ADD( now( ) , INTERVAL '12:30' HOUR_MINUTE )
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
hoping you guys can help - I have a timestamp column in my db table and i want to run a query to find any records from that table where the timestamp is the same as 7 days from now (ignoring the hours, minutes and seconds). So far I have this but I'm certain it's not what i should be doing:
WHERE `import_date` = 'DATE_SUB(NOW(), INTERVAL 7 days)'
WHERE import_date = 'DATE_SUB(NOW(), INTERVAL 7 days)'
should be
WHERE import_date >= DATE_SUB(NOW(), INTERVAL 7 day)
days should be day
for timestamp same as 7 day before from now use
WHERE DATE_FORMAT(`import_date`, "%Y-%m-%d") = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 day), "%Y-%m-%d")