Please help me to get the number of days left in the quarter from current date in SQL
Thanks
You can create quarter end date and then do datediff as follows:
SELECT DATEDIFF(MAKEDATE(YEAR(CURDATE()), 1)
+ INTERVAL QUARTER(CURDATE()) QUARTER
- INTERVAL 1 DAY,
CURDATE())
Related
I need a sql query to find the data from a employee table.
Startdate is present in employee table.
I need to add 90 days in startdate and then I need to check if the startdate lies in the current month or not.
I did try below query:
SELECT *
FROM `employees`
WHERE DATE_ADD(str_to_date(startdate, '%m/%d/%Y'),INTERVAL 90 DAY) BETWEEN '09/01/2016' AND '09/30/2016'
but its not giving me the expected results.(I do have data which should show up if the query is correct.)
Hi i did change the query and ran, please see the result. I am getting results of next month too :(
This is the query
SELECT id,startdate from employees WHERE str_to_date(startdate, '%m/%d/%Y') between DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day)
Query output
Rephrased my query...
Where the event was within the dates 90 days before the start and end of this month
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day) -- End of this month - 90 days
or, for three months
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 3 month) -- start of this month - 3 months
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 3 month) -- End of this month - 3 months
How do I SELECT the first week of a previous month I've tried
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>=(CURDATE()- 1 WEEK - INTERVAL 2 week)";
This was used to try and select the third week but this didn't work
Does this work for you:
$myQuery = "SELECT repairId , startDate,catId,statusId FROM repair
WHERE supermarketId = '$supermarket'
AND startDate>= curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week
AND startDate < curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day
- interval (day(curdate() - interval 1 month
- interval weekday(curdate() - interval 1 month) day) div 7) week _+ interval 1 week";
?
The idea here is that we first go back a month, then find the start of the week (assuming Monday, for Sunday we will need some extra tweaking), then figure out how many whole weeks it has been from the start of the month, and subtract that many weeks from the date so far. This takes us back to the start of the first week of the month. For the end of the range we just add one week to the start.
Ah, your question became more detailed.
Not really familiar with sql, there might be better but something like:
SELECT repairId , startDate,catId, statusId FROM repair
WHERE EXTRACT(YEAR_MONTH FROM start_date) = EXTRACT(YEAR_MONTH FROM NOW()) - 1 AND CAST(EXTRACT(DAY FROM start_date) / 7 + 1 as INT) = ?;
Basically, extract the year month components to compare year and month and then extract the day of month use the flooring caused by integer truncation to get the week to compare with whatever week you are looking for
mysql return rows matching year month
I need help calculating the following in MYSQL
how many days in quarter based on current date in MYSQL?
If today is 8/27/2014 - how many days is that in the quarter?
Also how do I calculate how many days in a quarter between two dates in MYSQL?
If user chose 3/1/2014 - 8/27/2014 how many total days for those 3 quarters?
to get the number of days in this quarter use this.
SELECT
DATEDIFF(
MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE()) QUARTER - INTERVAL 1 DAY,
MAKEDATE(YEAR(CURDATE()), 1) + INTERVAL QUARTER(CURDATE()) QUARTER - INTERVAL 1 QUARTER
)
to get the difference between two dates just use this
SELECT DATEDIFF(2014-08-27','2014-03-01')
im not sure why you would be doing this in mysql.. there are built in functions in other languages to do this same operation. so I wouldn't recommend doing it in MySQL.
This is what I was looking for:
select datediff(MAKEDATE(YEAR(CurDate()), 1)
+ INTERVAL QUARTER(CurDate()) QUARTER - INTERVAL 1 QUARTER,
case when Quarter(CurDate()) = quarter(curdate())
then CurDate() -- get the number of days in quarter up to today date
else
-- return number of days in quarter totally
DATE (DATE_SUB( DATE_ADD( CONCAT( YEAR( CurDate() ), '-01-01'),
INTERVAL QUARTER(CurDate()) QUARTER ), INTERVAL 1 DAY)) end) * -1 days_in_quarter
I am trying to find out how many times Kiln stopped in last three months. I have the following query:
SELECT SUM(kiln_no_stops) from monthly_report
where date BETWEEN DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%m-%Y')
AND DATE_FORMAT(NOW() , '%m-%Y')
When I use this query I get this error
Unknown column 'date' in 'where clause'.
But I am able to get the data for last one month using the query below:
SELECT SUM(kiln_no_stops)
from monthly_report
where date_format(yesterday,'%m-%Y')=Date_format(NOW() - INTERVAL 1 MONTH,'%m-%Y')
How can I get the data "Total number of stops" for the last three months?
Check if this would help you:
SELECT SUM(kiln_no_stops)
from monthly_report
where yesterday >= now()-interval 3 month;
EDIT:
To get last 3 months data (March1 to May31, current date = June 18)
SELECT SUM(kiln_no_stops)
from monthly_report where month(yesterday) < month(now())
AND yesterday >= cast( (last_day(now()) + interval 1 day - interval 4 month) as date);
I have date time field called transaction_date, in a report i need to select last calendar month, how do i do this ? (this should work for a month like January too)
I came up with following but this only works if the month is NOT january,
SELECT SUM(amount) AS pay_month FROM `users_payment` WHERE MONTH(transaction_datetime)= MONTH(NOW()) AND YEAR(transaction_datetime)=YEAR(NOW())
there are lot of examples using INTERVAL functions but this only select the time interval not the calendar month as i wanted too..
like
SELECT SUM(amount) AS `year_month` FROM `users_payment` WHERE DATE_ADD(NOW(), INTERVAL -1 MONTH) < transaction_datetime
but this is not what i want, i want to select sales sum of the DECEMBER only last year (remember there are other years too in the table which i dont want i.e 1979, 1981...etc)
same report next section, i need to select last 2 calender months, I dont know have any idea on how to do this too.
Have you tried the following
SELECT SUM(amount) AS `year_month` FROM `users_payment`
WHERE MONTH(DATE_ADD(NOW(), INTERVAL -1 MONTH)) = MONTH(transaction_datetime)
The above should work to show previous month; it does not distinguish between years however.
On second thought, I see what you are trying to do - To get all the transactions for a given month. Try something like this instead.
SELECT SUM(amount) AS `year_month` FROM `users_payment`
WHERE transaction_datetime BETWEEN date_format(NOW() - INTERVAL 1 MONTH, '%Y-%m-01')
AND last_day(NOW() - INTERVAL 1 MONTH)
This will list all the transactions for the previous calendar month. Alter the INTERVAL values to select multiple months.
You can try this--
SELECT SUM(amount) AS pay_month FROM `users_payment` WHERE
PERIOD_ADD(DATE_FORMAT(NOW(),'%Y%m'), -1) = DATE_FORMAT(transaction_datetime,'%Y%m')