Convert iso_week to calendar date in SQL - sql

I've been searching though the archives without finding what I am looking for- I'd be happy for some guidance.
I have a data set where I want to report aggregated number of appointments by provider (STAFFID) and work week, the latter defined by the week's Monday date. I've played with datepart(iso_week, appointment_date) as week_of_yr which gets me part of the way there- I can group by week to get the right numbers. However, I can't figure out if there's a simple way to display the date of the week's Monday given the iso_week integer (and year).
I found ISO8601 Convert Week Date to Calendar Date helpful, though I do not know whether (or how) I can automate that process for many values at once.
Here's the tidbit of code I have. Ideally I could add another expression to the select statement which would display the desired date.
select STAFFID
, count(*) as appointment_ct
, datepart(iso_week, appointment_date) as iso_wk --this returns the week # of the year as an int
from [dbo].[view_APPT_DATA]
where program_code in ('99999')
and appointment_date >= '1/1/2016' and appointment_date <='3/31/2016'
group by iso_wk, STAFFID

I would find the first Monday of that year and then use DATEADD to add the number of weeks to that day
select STAFFID
, count(*) as appointment_ct
, datepart(iso_week, appointment_date) as iso_wk --this returns the week # of the year as an int
, dateadd(week, datepart(week, DATEADD(DAY, (##DATEFIRST - DATEPART(WEEKDAY, dateadd(year, datepart(year, appointment_date) - 1900, 0)) + (8 - ##DATEFIRST) * 2) % 7, dateadd(year, datepart(year, appointment_date) - 1900, 0))) as monday_wk
from [dbo].[view_APPT_DATA]
where program_code in ('99999')
and appointment_date >= '1/1/2016' and appointment_date <='3/31/2016'
group by iso_wk, STAFFID, monday_wk

I didn't quite get the cha's query to return correct results for "special" years that have overlapping weeks.
This is the (inline) function i ended up with to calculate iso week to first day of that week:
CREATE OR ALTER FUNCTION dbo.FN_ISOWEEK_TO_DAY (
-- ISOWeek in format YYYYWW
#pISOWeek INT
)
RETURNS TABLE AS RETURN
SELECT DATEADD(week, CAST(RIGHT(#pISOWeek, 2) AS INT) - 1
- CASE WHEN (##DATEFIRST - DATEPART(WEEKDAY, DATEADD(YEAR, cast(LEFT(#pISOWeek, 4) AS INT) - 1900, 0)) + (8 - ##DATEFIRST) * 2) % 7 >= 4 -- means first monday is one week ahead
THEN 1 ELSE 0 END
, DATEADD(DAY, (##DATEFIRST - DATEPART(WEEKDAY, DATEADD(YEAR, cast(LEFT(#pISOWeek, 4) AS INT) - 1900, 0)) + (8 - ##DATEFIRST) * 2) % 7, DATEADD(YEAR, cast(LEFT(#pISOWeek, 4) AS INT) - 1900, 0)))
AS firstDay
Some test code:
SELECT *
FROM (
SELECT 202001, 20191230
UNION ALL
SELECT 202053, 20201228
) x (dt, expectedValue)
CROSS APPLY dbo.FN_ISOWEEK_TO_DAY(x.dt) y

Related

How to extract 'Week Number' and 'Day Number' from Date if Start Day is 'Monday' in a Month SQL

I am extracting below from a date:
Week Number
Day Number
Where start day of my Week is Monday.
For Example the date is : '1st May 2019' or '2019-05-01'
Then
Week Number = 1
Day Number = 3
Because the day is Wednesday on 1st May 2019.
I am using MS SQL Server 2012.
and used below code from net to get Week Number.
I get the Week Number from this but i do not understand what actually it is doing.
select datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, #date), 0)), 0), #date - 1) + 1
Can anyone explain it to me and tell me how to extract the Day Number.
This select will give you the #date day of week (1: Monday, 7: Sunday), regardless of your ##DATEFIRST set MS Doc:
select (DATEPART(DW, #date) + ##DATEFIRST + 5) % 7 + 1 as [Day of Week]
If you set datefirst, you can simply do:
set datefirst 1; -- "1" = "Monday"
select datepart(week, getdate()), datepart(weekday, getdate())
Others have mentioned how to get the day-of-week.
In terms of your code for getting week-of-month:
select datediff(week, dateadd(week, datediff(week, 0, dateadd(month, datediff(month, 0, #date), 0)), 0), #date - 1) + 1
To my eye it appears to get the number of full months elapsed between day zero and the current date, giving us a month-serial.
Then adds zero months to this figure (which is apparently being used to cast the month-serial back to a date representing the beginning of the current month), giving us a beginning-of-month date.
It then gets the number of full weeks elapsed between day zero and the beginning-of-month date, giving us a week-serial.
The week-serial is then cast back to a date representing the beginning-of-week date, representing the Monday on or prior to the beginning-of-month date.
It then gets the difference, in weeks, between the monday-on-or-prior date and the day before the current date, and adds 1 (so that the first week of the month is represented as Week 1).
It sounds like a plausible algorithm, but I wouldn't endorse it without testing and inspecting the intermediary values, however, or unless you have found it from an authoritative source.
Check This-
SELECT ISNULL(NULLIF(DATEPART(DW,GETDATE())-1,0),7)
I have constructed the query as per your need. It works fine.
NOTE : Sunday is considered as 1st day of the week and Saturday is considered as 7th day of the week
DECLARE #dt datetime='2019-05-01'
DECLARE #Weekday INT
if(((DATEPART(DW, #dt) + ##DATEFIRST + 5) % 7 +2) =8)
SET #Weekday =1
else
SET #Weekday = ((DATEPART(DW, #dt) + ##DATEFIRST + 5) % 7 +2)
select DATEPART(week, #dt)- DATEPART(week, CONVERT(CHAR(6), #dt, 112)+'01')+ 1 AS [Week Number], #Weekday AS [Day of Week]

Not another SQL time between dates minus weekends question

I have two dates: CREATION_DATE and START_DATE. START_DATE will always be later than CREATION_DATE. I need to calculate the number of minutes between them, except for minutes which happen on a weekend.
Every solution I can find assumes one of those dates occurs on a weekend, but alas, if CREATION_DATE is on a Friday, and START_DATE is a Monday, all of Saturday and Sunday is counted.
I've even tried calculating minutes from CREATION_DATE to the next 12am occurs plus minutes from first 12am Monday to START_DATE, but that doesn't work either.
I have found a solution if I only wanted to count days. I need to know down to minutes.
Our DB is hosted an I am not able to create VB functions so my solution must be all SQL.
The basic idea is to generate a record for all minutes between the start and finish, including those on weekends. Then use the WHERE clause to filter out those you don't want. In many cases, this is done by joining to a Calendar table, so you can also look at holidays or other special events, but for this purpose we can just use the DATEPART() function.
One this is done, we use a GROUP BY to roll things back up to the original date values and the COUNT() function to know how much work we did.
This basic concept works whether you're counting days, minutes, months, whatever.
It's not clear in the question, but I'm gonna assume your start and end values are columns in a table, rather than variable names (no #).
WITH Numbers(Number) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY s1.[object_id]) - 1
FROM sys.all_columns AS s1
CROSS JOIN sys.all_columns AS s2
)
SELECT t.CREATION_DATE, t.START_DATE, COUNT(*) AS Num_Minutes
FROM [MyTable] t
INNER JOIN Numbers n on n.Number <= DATEDIFF(minute, t.CREATION_DATE, t.START_DATE)
WHERE DATEPART(dw, DATEADD(minute, n.Number, t.CREATION_DATE)) NOT IN (7,1)
GROUP BY t.CREATION_DATE, t.START_DATE
But this has the potential to be very slow, depending on how far apart the dates are. You can improve this by using various other ways to generate the Numbers table to get a starting point that better approximates the needs of your actual data.
If you aren't worried about accounting for holidays, you can do this as a simple math problem without having to monkey around with a tally table or doing any counting.
The following works by dropping the time portion off the begin and end date parameters and calculates the number of working days from that and multiples that figure by 3660. From there, if the begin date is a week day the begin date mins are subtracted... if end date is a weekday, those mins are added.
DECLARE
#BegDate DATETIME = '2018-09-13 03:30:30',
#EndDate DATETIME = '2018-09-18 03:35:27';
SELECT
working_mins = bm.base_mins
- ((1 - (x.is_beg_sat + x.is_beg_sun)) * x.beg_mins) -- if the begin date is a week day, subtract the mins from midnight.
+ ((1 - (x.is_end_sat + x.is_end_sun)) * x.end_mins) -- if the end date is a week day add the mins from midnight.
--,*
FROM
( VALUES (
DATEADD(DAY, DATEDIFF(DAY, 0, #BegDate), 0),
DATEADD(DAY, DATEDIFF(DAY, 0, #EndDate), 0)
) ) d (beg_date, end_date)
CROSS APPLY ( VALUES (
DATEDIFF(MINUTE, d.beg_date, #BegDate),
DATEDIFF(MINUTE, d.end_date, #EndDate),
DATEDIFF(DAY, d.beg_date, d.end_date),
DATEDIFF(WEEK, d.beg_date, d.end_date) * 2,
1 - SIGN(DATEPART(WEEKDAY, d.beg_date) % 7),
1 - SIGN(DATEPART(WEEKDAY, d.end_date) % 7),
1 - SIGN((DATEPART(WEEKDAY, d.beg_date) + 7) % 8),
1 - SIGN((DATEPART(WEEKDAY, d.end_date) + 7) % 8)
) ) x (beg_mins, end_mins, total_days, weekend_days, is_beg_sat, is_end_sat, is_beg_sun, is_end_sun)
CROSS APPLY ( VALUES (1440 * (x.total_days - x.weekend_days + x.is_beg_sat - x.is_end_sat)) ) bm (base_mins);
You can take a look at this solution, and see if meets your needs. Basically, I did the following:
Take the number of whole days betwen StartDate and EndDate that aren't weekend days, and multiply by 2:
SELECT COUNT(*) * 24 * 60 FROM WholeDaysBetween WHERE wkday <= 5
Take the minutes from StartDate (hours*60 + minutes)
(24 * 60) - (DATEPART(HOUR, #StartDate) * 60) - (DATEPART(MINUTE, #StartDate))
Take the minutes from EndDate (hours*60 + minutes)
(DATEPART(HOUR, #EndDate) * 60) + (DATEPART(MINUTE, #EndDate))
To get the number of whole days between, I used a recursive CTE:
WITH
WholeDaysBetween(dt, wkday) AS
(
SELECT DATEADD(DAY, 1, #StartDate), DATEPART(WEEKDAY, DATEADD(DAY, 1, #StartDate))
UNION ALL
SELECT DATEADD(DAY, 1, dt), DATEPART(WEEKDAY, DATEADD(DAY, 1, dt))
FROM WholeDaysBetween
WHERE dt < DATEADD(DAY, -1, #EndDate)
)
Of course, for this to work, you have to adjust your datefirst settings.
The final query is as follows (I used the same sample data as in your comment):
set datefirst 1; -- day starts on Monday
declare #StartDate datetime = '2018-09-21 23:59:00';
declare #EndDate datetime = '2018-09-24 00:01:00';
WITH
WholeDaysBetween(dt, wkday) AS
(
SELECT DATEADD(DAY, 1, #StartDate), DATEPART(WEEKDAY, DATEADD(DAY, 1, #StartDate))
UNION ALL
SELECT DATEADD(DAY, 1, dt), DATEPART(WEEKDAY, DATEADD(DAY, 1, dt))
FROM WholeDaysBetween
WHERE dt < DATEADD(DAY, -1, #EndDate)
)
SELECT
-- whole weekdays between #StartDate and #EndDate,
-- multiplied by minutes per day
(
SELECT COUNT(*) * 24 * 60
FROM WholeDaysBetween
WHERE wkday <= 5
)
+
-- minutes from #StartDate date to end of #StartDate
-- as long as #StartDate isn't on weekend
(
SELECT
CASE
WHEN DATEPART(WEEKDAY, #StartDate) <= 5
THEN
(24 * 60) -
(DATEPART(HOUR, #StartDate) * 60) -
(DATEPART(MINUTE, #StartDate))
ELSE 0
END
)
+
-- minutes from start of #EndDate's date to #EndDate
-- as long as #EndDate isn't on weekend
(
SELECT
CASE
WHEN DATEPART(WEEKDAY, #EndDate) <= 5
THEN
(DATEPART(HOUR, #EndDate) * 60) +
(DATEPART(MINUTE, #EndDate))
ELSE 0
END
)

T-SQL Dynamic date range in WHERE clause (Last fiscal year + year to date)

I'm using the following WHERE clause to only load records from the last fiscal year plus a year to date.
Running without the WHERE clause, it takes 30seconds for 1mil records. With the WHERE clause, I had to stop it after 2hours.
Can you please share your thoughts
WHERE
([schema].[table].[DATE] >= DATEADD
(yy, - 1, DATEADD
(MONTH,(MONTH(GETDATE()) - 1) / 6 * 12 - 6,
CAST(CAST(YEAR(GETDATE()) AS VARCHAR) AS DATETIME)
)
)
)
Declare #PastFiscalYear Date
Set #PastFiscalYear= DATEADD(yy, - 1, DATEADD(MONTH,(MONTH(GETDATE()) - 1) / 6 * 12 - 6,CAST(CAST(YEAR(GETDATE()) AS VARCHAR) AS DATETIME)))
WHERE
([schema].[table].[DATE] >= #PastFiscalYear )
Can you try this
This will bring back data since the previous fiscal year start date. Just change the -3 to what ever your FY offset is. -3 is for October.
[schema].[table].[DATE] >= DATEADD(mm,-3,DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(YEAR, -1, GETDATE())), 0))

default date value of every Friday in SQL Server?

In SQL Server 2008 i want to set a default date value of every Friday to show up in the column when i insert a new record?
ALTER TABLE myTable ADD CONSTRAINT_NAME DEFAULT GETDATE() FOR myColumn
Whats the best way to show every Friday?
I want the default value to be based on the now date then knowing that the next available date is 05-07/2013
I have the following:
dateadd(d, -((datepart(weekday, getdate()) + 1 + ##DATEFIRST) % 7), getdate())
But when passing todays date, it gave me: 2013-06-28 which is actually LAST Friday!, it should be the up and coming Friday!
SELECT DATEADD(day,-3, DATEADD(week, DATEDIFF(week, 0, current_timestamp)+1, 0)) AS LastFridayDateOfWeek
Gets the last date of current week (sunday) then subtracts 3 from that to get Friday.
Replace current_timestamp if you need a different dates friday.
EDIT:
I thought about this a bit, and if the above (Friday THIS WEEK, so for Saturday it gives the previous date) does not work, you could easily use a reference date set like so:
DATEADD(DAY,7 + DATEDIFF(day,'20100109',#checkDateTime)/7*7,'20100108') as FridayRefDate
Same thing but with no hard coded Friday/Saturday in it:
DATEADD(DAY,7 + DATEDIFF(day,DATEADD(wk, DATEDIFF(wk,0,#checkDateTime),5),#checkDateTime)/7*7,DATEADD(wk, DATEDIFF(wk,0,#checkDateTime), 4))
So for 20100109 is a Friday.
SET #checkDateTime = '2012-01-14 3:34:00.000'
SELECT DATEADD(DAY,7 + DATEDIFF(day,'20100109',#checkDateTime)/7*7,'20100108') as FridayRefDate
it returns "2012/1/20"
But for SET #checkDateTime = '2012-01-13 3:34:00.000' it returns "2012/1/13"
If your current query gives you last Friday, the easiest thing to do is simply to add 7 to it:
select dateadd(d, 7-((datepart(weekday, getdate()) + 1 + ##DATEFIRST) % 7), getdate())
------------------^
SELECT CONVERT(DATE, ( CASE WHEN DATEPART(dw, GETDATE()) - 6 <= 0
THEN DATEADD(dd,
( DATEPART(dw, GETDATE()) - 6 ) * -1,
GETDATE())
ELSE DATEADD(dd, ( DATEPART(dw, GETDATE()) ) - 1,
GETDATE())
END )) AS NearestFriday
Just add 7 to the formula
SELECT DATEADD(dd,CAST(5-GETDATE() AS int)%7,GETDATE()+7)
To verify the formula:
WITH test AS (
SELECT GETDATE() AS d UNION ALL
SELECT DATEADD(dd,1,d)
FROM test WHERE d < GETDATE() + 30
)
SELECT
d AS [input],
DATEADD(dd,CAST(5-d AS int)%7,d+7) AS [output]
FROM test
To tweak the the formula, adjust the 5- and the +7

Group days by week

Is there is a way to group dates by week of month in SQL Server?
For example
Week 2: 05/07/2012 - 05/13/2012
Week 3: 05/14/2012 - 05/20/2012
but with Sql server statement
I tried
SELECT SOMETHING,
datediff(wk, convert(varchar(6), getdate(), 112) + '01', getdate()) + 1 AS TIME_
FROM STATISTICS_
GROUP BY something, TIME_
ORDER BY TIME_
but it returns the week number of month. (means 3)
How to get the pair of days for current week ?
For example, now we are in third (3rd) week and I want to show 05/14/2012 - 05/20/2012
I solved somehow:
SELECT DATEADD(ww, DATEDIFF(ww,0,<my_column_name>), 0)
select DATEADD(ww, DATEDIFF(ww,0,<my_column_name>), 0)+6
Then I will get two days and I will concatenate them later.
All right, bear with me here. We're going to build a temporary calendar table that represents this month, including the days from before and after the month that fall into your definition of a week (Monday - Sunday). I do this in a lot of steps to try to make the process clear, but I probably haven't excelled at that in this case.
We can then generate the ranges for the different weeks, and you can join against your other tables using that.
SET DATEFIRST 7;
SET NOCOUNT ON;
DECLARE #today SMALLDATETIME, #fd SMALLDATETIME, #rc INT;
SELECT #today = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0), -- today
#fd = DATEADD(DAY, 1-DAY(#today), #today), -- first day of this month
#rc = DATEPART(DAY, DATEADD(DAY, -1, DATEADD(MONTH, 1, #fd)));-- days in month
DECLARE #thismonth TABLE (
[date] SMALLDATETIME,
[weekday] TINYINT,
[weeknumber] TINYINT
);
;WITH n(d) AS (
SELECT TOP (#rc+12) DATEADD(DAY, ROW_NUMBER() OVER
(ORDER BY [object_id]) - 7, #fd) FROM sys.all_objects
)
INSERT #thismonth([date], [weekday]) SELECT d, DATEPART(WEEKDAY, d) FROM n;
DELETE #thismonth WHERE [date] < (SELECT MIN([date]) FROM #thismonth WHERE [weekday] = 2)
OR [date] > (SELECT MAX([date]) FROM #thismonth WHERE [weekday] = 1);
;WITH x AS ( SELECT [date], weeknumber, rn = ((ROW_NUMBER() OVER
(ORDER BY [date])-1) / 7) + 1 FROM #thismonth ) UPDATE x SET weeknumber = rn;
-- now, the final query given all that (I've only broken this up to get rid of the vertical scrollbars):
;WITH ranges(w,s,e) AS (
SELECT weeknumber, MIN([date]), MAX([date]) FROM #thismonth GROUP BY weeknumber
)
SELECT [week] = CONVERT(CHAR(10), r.s, 120) + ' - ' + CONVERT(CHAR(10), r.e, 120)
--, SOMETHING , other columns from STATISTICS_?
FROM ranges AS r
-- LEFT OUTER JOIN dbo.STATISTICS_ AS s
-- ON s.TIME_ >= r.s AND s.TIME_ < DATEADD(DAY, 1, r.e)
-- comment this out if you want all the weeks from this month:
WHERE w = (SELECT weeknumber FROM #thismonth WHERE [date] = #today)
GROUP BY r.s, r.e --, SOMETHING
ORDER BY [week];
Results with WHERE clause:
week
-----------------------
2012-05-14 - 2012-05-20
Results without WHERE clause:
week
-----------------------
2012-04-30 - 2012-05-06
2012-05-07 - 2012-05-13
2012-05-14 - 2012-05-20
2012-05-21 - 2012-05-27
2012-05-28 - 2012-06-03
Note that I chose YYYY-MM-DD on purpose. You should avoid regional formatting like M/D/Y especially for input but also for display. No matter how targeted you think your audience is, you're always going to have someone who thinks 05/07/2012 is July 5th, not May 7th. With YYYY-MM-DD there is no ambiguity whatsoever.
Create a calendar table, then you can query week numbers, first/last days of specific weeks and months etc. You can also join on it queries to get a date range etc.
How about a case statement?
case when datepart(day, mydatetime) between 1 and 7 then 1
when datepart(day, mydatetime) between 8 and 14 then 2
...
You'll also have to include the year & month unless you want all the week 1s in the same group.
It's not clear of you want to "group dates by week of month", or alternately "select data from a given week"
If you mean "group" this little snippet should get you 'week of month':
SELECT <stuff>
FROM CP_STATISTICS
WHERE Month(<YOUR DATE COL>) = 5 --april
GROUP BY Year(<YOUR DATE COL>),
Month(<YOUR DATE COL>),
DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, <YOUR DATE COL>), 0)
, <YOUR DATE COL>) +1
Alternately, if you want "sales for week 1 of April, ordered by date" You could do something like..
DECLARE #targetDate datetime2 = '5/3/2012'
DECLARE #targetWeek int = DATEDIFF(week, DATEADD(MONTH,
DATEDIFF(MONTH, 0, #targetDate), 0), #targetDate) +1
SELECT <stuff>
FROM CP_STATISTICS
WHERE MONTH(#targetDate) = Month(myDateCol) AND
YEAR(#targetDate) = Year (myDateCol) AND
#targetWeek = DATEDIFF(week, DATEADD(MONTH,
DATEDIFF(MONTH, 0, myDateCol), 0), myDateCol) +1
ORDER BY myDateCol
Note, things would get more complicated if you use non-standard weeks, or want to reach a few days into an earlier month for weeks that straddle a month boundary.
EDIT 2
From looking at your 'solved now' section. I think your question is "how do I get data out of a table for a given week?"
Your solution appears to be:
DECLARE #targetDate datetime2 = '5/1/2012'
DECLARE #startDate datetime2 = DATEADD(ww, DATEDIFF(ww,0,targetDate), 0)
DECLARE #endDate datetime2 = DATEADD(ww, DATEDIFF(ww,0,#now), 0)+6
SELECT <stuff>
FROM STATISTICS_
WHERE dateStamp >= #startDate AND dateStamp <= #endDate
Notice how if the date is 5/1 this solution results in a start date of '4/30/2012'. I point this out because your solution crosses month boundaries. This may or may not be desirable.