If complaint filed on 5 pm on Saturday and closed on 1 pm on Monday, ticket should exclude sunday 24 hours.
Complaint Creation Date:24 Sept 2014, 6:00 PM Sat
Complaint Closed Date : 26 Sept 2014 ,6:00 PM Monday
Time taken :1 day not 2 day
Need Suggestions..
You can try somthing like this:-
SELECT CASE WHEN DAYNAME(DATE_ADD(Complaint_Creation_Date, INTERVAL 1 day)) = 'Sunday'
THEN (TIMESTAMPDIFF(HOUR,Complaint_Creation_Date,Complaint_Closed_Date))/24 - 1 AS "TIME TAKEN"
ELSE
(TIMESTAMPDIFF(HOUR,Complaint_Creation_Date,Complaint_Closed_Date))/24 AS "TIME TAKEN"
END
FROM YOUR_TABLE;
Related
My company’s week begins on a Sunday.
The issue I’m having is extracting the week number from the date in the database.
For example,02/01/2022 falls in week 52 of 2021 using the Sunday week beginning logic. How can I write a SQL code to give me :
a) the week number (I.e 2nd Jan 2022 is part of week 52)
b) the correct year of the week. i.e 2nd Jan 2022 , I want it to bring back the year “2021” so my data set is complete and accurate.
N.b.
Database name I’m using is dw-fin and column of the date is called date-created
You can use the WEEK function with a 'mode'.
Which is an integer indicating the starting of the week.
And the YEARWEEK function also has a mode.
Mode
First day of week
Range
Week 1 is the first week
0
Sunday
0-53
with a Sunday in this year
1
Monday
0-53
with 4 or more days this year
2
Sunday
1-53
with a Sunday in this year
3
Monday
1-53
with 4 or more days this year
4
Sunday
0-53
with 4 or more days this year
5
Monday
0-53
with a Monday in this year
6
Sunday
1-53
with 4 or more days this year
7
Monday
1-53
with a Monday in this year
Example :
select date_column
, month(date_column) as month
, year(date_column) as year
, yearweek(date_column, 2) as mode2_yearweek
, floor(yearweek(date_column, 2)/100) as mode2_year
, week(date_column, 2) as mode2_week
from (select date('2022-01-01') as date_column) q;
date_column
month
year
mode2_yearweek
mode2_year
mode2_week
2022-01-01
1
2022
202152
2021
52
Demo on db<>fiddle here
I am having below table structure where day_index 0 = Monday, 1 = Tuesday and 6 = Sunday. Here start_time and end_time is UTC Time.
day_index hour_string start_time end_time
0 05:30 AM - 05:30 PM 07:30 PM 07:30 AM
1 10:00 AM - 08:00 PM 12:00 AM 10:00 AM
2 07:30 AM - 11:30 PM 09:30 PM 01:30 PM
3 09:00 AM - 02:30 PM 11:00 PM 04:30 AM
4 09:00 AM - 04:00 PM 11:00 PM 06:00 AM
5 08:30 AM - 03:30 PM 10:30 PM 05:30 AM
6 08:30 AM - 11:00 PM 10:30 PM 01:00 PM
Now based on today's time i need whole weekday like today we have 22-07-2020 so it would be 20-07-2020,21-07-2020 and last one is 27-07-2020.
Also if today times is over ( end_time with timezone ) i need next available day with start_time.
Example : In today's case we have 1.30 PM UTC + 5.30 = 6.30. So we have next opening time would be 11.00 PM UTC + 5.30 = 4.30 AM In Morning.
Are you familiar with CONVERT_TZ? That is how you convert a datetime to another timezone. You cannot convert a time without the date to a timezone.
Here are some variables to set before running the query;
SET #last_monday = DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY);
SET #local_timezone = 'US/Eastern';
This query will give you the date for each day of the week, the start and end time in UTC, and the start and end time in the local timezone:
SELECT DATE_ADD(#last_monday, INTERVAL day_index DAY) date_dayofweek,
TIMESTAMP(CONCAT(DATE_ADD(#last_monday, INTERVAL day_index DAY),' ',start_time)) startDateTimeUTC,
TIMESTAMP(CONCAT(DATE_ADD(#last_monday, INTERVAL day_index DAY),' ',end_time)) endDateTimeUTC,
CONVERT_TZ(TIMESTAMP(CONCAT(DATE_ADD(#last_monday, INTERVAL day_index DAY),' ',start_time)),'UTC',#local_timezone) startDateTimeLocal,
CONVERT_TZ(TIMESTAMP(CONCAT(DATE_ADD(#last_monday, INTERVAL day_index DAY),' ',end_time)),'UTC',#local_timezone) endDateTimeLocal
FROM table_name ;
Using the answer here to get last Monday -
MYSQL: How can I find 'last monday's date' (performance Issue)
You can also use variables within the query to make this query much more readable, as there is a lot of repetition. I'm not sure if that would be confusing for you, but I'm happy to provide you with a query if you would need it.
This was a tricky mysql query but with the help of another answer it is quite staightforward to answear.
SELECT
DATE_ADD(DATE_ADD(CURDATE(),
INTERVAL - WEEKDAY(CURDATE()) DAY),
INTERVAL day_index DAY)
FROM
schedule
I have date column with list of dates stored i want to get every Saturday to Thursday data on every friday using mysql how can i do this?
i tried with this query but don't know how to do with my requirements
SET DATEFIRST 1 -- Define beginning of week as Monday
SELECT [...]
AND WorkDate >= dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate()))
AND WorkDate < dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
You can refer this query for get data from Saturday to Thursday.
1 Day= 86400 seconds // so you can set INTERVAL according to day range
Note: This is logic only, you can manage your query. MySQL already having functions like DAYOFWEEK(), DATE(NOW())
I'm refering this link for futher details: Date & Time Function in MySQL
SELECT
satwk_beg + INTERVAL 0 second sat_beg,
satwk_beg + INTERVAL 518399 second thu_end
FROM (SELECT (DATE(NOW()) - INTERVAL daysbacktothursday DAY) satwk_beg
FROM (SELECT SUBSTR('1234560',wkndx,1) daysbacktothursday
FROM (SELECT DAYOFWEEK(dt) wkndx FROM (SELECT DATE(NOW()) dt) AAAA) AAA) AA) A;
Just execute above query and check you will get output from Saturday to Thursday.
Here is range of other weeks,
(SELECT SUBSTR('6012345',wkndx,1) does the week starting Mon ending Sun
(SELECT SUBSTR('5601234',wkndx,1) does the week starting Tue ending Mon
(SELECT SUBSTR('4560123',wkndx,1) does the week starting Wed ending Tue
(SELECT SUBSTR('3456012',wkndx,1) does the week starting Thu ending Wed
(SELECT SUBSTR('2345601',wkndx,1) does the week starting Fri ending Thu
(SELECT SUBSTR('1234560',wkndx,1) does the week starting Sat ending Fri
(SELECT SUBSTR('0123456',wkndx,1) does the week starting Sun ending Sat
I am trying to create a weekly report (including grouping by WEEK(date)) where weeks start on Monday. However, Week 1 is always the first week any day of a new year occurs. For example if Jan 1st is a Friday, that is week one.
This doesn't seem to be consistent with MySQL mode options for WEEK():
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
Based on the available options it only works if the week has 4 or more days (options 1 and 3) or the Monday is in the new year (options 5 and 7). Neither of these account for example week 1 of 2016:
Wk Mon Tue Wed Thu Fri Sat Sun
No.
1 28 29 30 31 1 2 3
Is there any way to order/number the week numbers in MySQL based on this custom week mode requirement?
Using MySQL, how do I find the next weekday/time combination after a given datetime?
For example, how do I select the next Sunday at 10AM given an input date of Tuesday 12 August 2014 6PM - which should return: Sunday 17 August 10AM?
SELECT DATE_ADD(#input_date, INTERVAL (8 - DAYOFWEEK(#input_date)) DAY) AS next_sunday
From: http://www.gizmola.com/blog/archives/99-finding-next-monday-using-mysql-dates.html
SELECT DATE_ADD('2014-08-12', INTERVAL 5 DAY) as NEXTSUNDAY;