Get Work week or Number of Weeks T-SQL - sql

How can I get the exact week number if my date range is from Friday to Thursday nextweek?
I used this code
datepart(wk,t.date) as workweek
but the week number is different, maybe because the format that I want to get is from Friday to Thursday nextweek. I hope somebody can answer. TIA!

SET DATEFIRST 5; -- Set Friday as first day of the week
Select datepart(wk,t.date) -1 as workweek
From yourTable as t

Related

Is there a way to get a range of dates from Saturday to Sunday of the previous week (SQL)

I have a SQL question. Is there a way to get a range of dates from Sunday to Saturday.
I saw individual dates were discussed before but I don't see the range of 7 days.
WHERE DATE BETWEEN #LastSunday AND #LastSaturday
If someone knows, would be great.

Get Monday of last week with JodaTime

I need get Monday of last week E.g
Today is 28/11/18 (wednesday)
I need get 19/11/18 (monday)
I know that I can obtain Monday of the current week with
val monday = DateTime().withDayOfWeek(DateTimeConstants.MONDAY)
I get it!
DateTime().withWeekOfWeekyear(
DateTime().weekOfWeekyear-1)
.withDayOfWeek(DateTimeConstants.MONDAY)
I only have to get the week of the year and subtract one week and that is all

Informix SQL to get date range that changes automatically

Please help me with an Informx SQL query to be run every Friday.
Period: 1st of the Month to the Thursday before the Friday that it is being run on. I can't just choose date ranges as it will be an auto report so the dates needs to update automatically. Is there a way to do this?
You've got two issues: the start of the month and the date of last Thursday. The simplest expression to identify the 1st day of the current month is:
MDY(MONTH(TODAY), 1, YEAR(TODAY))
The way to identify the most recent Thursday is via a CASE statement on WEEKDAY(TODAY). Something like:
CASE
WHEN WEEKDAY(TODAY) < 5 -- (Sunday (0) - Thursday (4))
THEN TODAY - WEEKDAY(TODAY) - 3 -- Calc last Sunday, back 3 days
ELSE TODAY - WEEKDAY(TODAY) + 4 -- Calc last Sunday, forward 4 days
END
Note, you will still run into issues where the 1st of current month is later than the most recent Thursday. But hopefully the examples above will point you in the right direction.
Of course, if you know this report is only ever going to be run on Fridays, then calculating the most recent Thursday is just TODAY -1.

What exactly does trunc(date, 'IW')?

For my project I need to have an absolute numerical correspondence between days of the week and 1...7 values.
As you probably know the association between days and numbers can vary according to the locale, for example in Germany Monday is 1 and Sunday is 7, while in US Monday is 2 while Sunday is 1.
So, searching for a solution, I found the following code which seems working regardless of the locale, assigning Monday=1...Sunday=7:
1 + TRUNC (date) - TRUNC (date, 'IW')
Can someone explain me how does it work? In particular I just can't understand what this instruction:
TRUNC (date, 'IW')
exactly does.
TRUNC(DATE,'IW') returns the first day of the week. For me TRUNC(SYSDATE,'IW) returns Monday. Today is Tuesday Feb 21. Subtract from that TRUNC(SYSDATE,'IW') which would be Monday the 20th, and you'll get 1 (because 21-20=1). Add 1 onto that as you do in the beginning of your equation and you get 2, which we associate with Tuesday.
The very basic concept of ISO week is to make it NLS territory independent.
From documentation,
Week of year (1-52 or 1-53) based on the ISO standard.
A week starts on a Monday and ends on a Sunday.

Using DatePart to show specific Days

Is it possible to use the DatePart function to show a Week running from Sat - Fri as opposed to your typical Monday - Sunday week? I know this will return Monday - Sunday, but can ya change it to Sat - Wed?
DATEPART(WEEK,[HireDate]) AS Week_Number
Yes use SET DATEFIRST this sets the day to count as the first day of the week so it changes on the day you specify.
You can alter the first day of the week:
SET DATEFIRST { number | #number_var }
You can change the first day of week with datefirst.
set datefirst 6
Datepart week and weekday will work according.
Check http://msdn.microsoft.com/en-us/library/ms174420.aspx for more details.