SQL Server: DATEFIRST Permanent set to Monday - sql-server

I am setting the DATEFIRST to start on Monday using this command.
SET DATEFIRST 1;
My question is: will this permanently set date first to Monday for the entire DB and users? Or will it only affect this one query?
If it only affects the query it is run with, what type of command would I need to permanently change the DATEFIRST to Monday for all users/entire DB?

The default first day of week is based on your system setting in Regional and Language setting. By default it is Sunday = 7 (U.S. English).
As Microsoft website shows, the days are as following:
Value First day of the week is
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday(default, U.S. English)
An example to know which day is the first day:
SELECT ##DATEFIRST AS 'First Day',
DATEPART(dw, SYSDATETIME()) AS 'Today'

Related

##DATEFIRST not returning results as described in the docs

I have read this documentation
So I tried this experiment
declare #t table (test date)
insert into #t values ('20220404'), ('20220405'),('20220406'),('20220407'),('20220408'),('20220409'),('20220410')
select datename(weekday, test),
datepart(weekday, test)
from #t
it returns this
COLUMN1
COLUMN2
Monday
2
Tuesday
3
Wednesday
4
Thursday
5
Friday
6
Saturday
7
Sunday
1
I checked my value or ##DATEFIRST
select ##DATEFIRST
it returns 7
So why do I not get this result then as described in the docs?
COLUMN1
COLUMN2
Monday
1
Tuesday
2
Wednesday
3
Thursday
4
Friday
5
Saturday
6
Sunday
7
EDIT
this is what I see in the docs
I think you may be misunderstanding the docs. The docs for DATEFIRST say, as you've seen:
Sets the first day of the week
So, the value of DATEFIRST determines which day gets numbered 1, the first day of the week. With DATEFIRST set to 7, as the table goes on to show, Sunday will be considered the first day of the week - day number 1.
With that setting, DATEPART for weekday will return 1 for any Sunday, because Sunday is considered the first day of the week.
It is perhaps unfortunate that numbers are used as the argument to SET DATEFIRST, since naturally this confusion arises. It might have been nice if we could say SET DATEFIRST Sunday to make it obvious what we mean, but unfortunately that's not the syntax.

sql code for week should start on monday end on sunday

Business starts from Monday and ends on Sunday as a complete week. So I need to group by week in SSRS. How can I do this?
I am attaching the screenshot of my requirement.
Any help will be greatly appreciated.
User SET DATEFIRST 1;
DATEFIRST will set the first day of the week according to value given. 1 is for Monday, 2 TUESDAY and so on.
It will always have a fixed value for days. Like 1 will always be Monday and 7 will always be Sunday.
SELECT ##DATEFIRST will give the Week start day. By default it will be 7 i.e. Sunday. So if you run SET DATEFIRST 1 then SELECT ##DATEFIRST will return 1.
You may have to put SET DATEFIRST 1 inside Stored Procedure along with your query.

Produce data extracts using day field for the Friday before the weekend

Using TSQL on SQL Server...
I need to produce extracts that use a pay day column in a database that only holds the day number, for example 26 to produce extracts for the 26th day of the month with a twist if that pay day falls on a weekend then the data for that extract should be extracted the Friday before the weekend.
Has anybody attempted this and able to offer some ways of achieving this through TSQL?
Thanks
You have said in today's comment above that you are assuming the current month.
First turn your payday day number into a proper date:
select dateadd(month,((year(getdate())-1900)*12)+month(getdate())-1,payday-1)
as paydate
and then use a case statement against it:
case datename(weekday,paydate)
when 'Saturday' then dateadd(day,-1,paydate)
when 'Sunday' then dateadd(day,-2,paydate)
else paydate
end as paydateadjust
(Obviously these will need rewriting into a proper SQL statement. Date formula borrowed from Michael Valentine Jones' super useful SQL Server date function.)
I'm assuming you have the month and year available too, in which case you can construct a real date, and get the day of week from it as a 1-based number starting at Sunday. So, 6 equals Friday. This will produce 5 for today, as Thursday = 5
SELECT DATEPART(dw, DATEFROMPARTS(2018, 07, 26)) AS [DayOfWeek]
EDIT:
I'll make another assumption, this time that this is somehow possible, like it's always run for the current month. This is a bit dodgy as it all falls apart if it's run too early or too late, but at least it's achievable.
SELECT DATEPART(dw, DATEFROMPARTS(
DATEPART(YEAR, GETDATE()),
DATEPART(MONTH, GETDATE()),
26)) AS [DayOfWeekThisMonth]
EDIT 2:
OK, with the understanding we are working on this month, we can do this:
DECLARE #PayDayNumber INT = 29; -- Replace this with a SELECT to get your value
SELECT CASE(DATEPART(dw, DATEFROMPARTS(DATEPART(YEAR, GETDATE()), DATEPART(MONTH, GETDATE()), #PayDayNumber)))
WHEN 1 THEN #PayDayNumber - 2
WHEN 7 THEN #PayDayNumber - 1
ELSE #PayDayNumber
END AS [WeekendSafePayDay]
For this month (August 2018) the 29th will return 29th. You can change DATEPART(MONTH, GETDATE()) for 9 to test September, which will return 28 because 29th September is a Saturday, or change it to 7 to test July, which will return 27 because 29th July is a Sunday.

Business Week Group in SQL (instead of calendar week)

My requirement is that I want to find business-week-ending (not the calender week) given a DATE column from the sales table in MSSQL.
Using different techniques I was able to find the [Calender] week-endings (and week-starting) dates corresponding to DATE in the table.
Since our business week ends on Wednesday [DOW 3 or 4 depending when the week started], I tried to deduct number of days from the week ending dates to pull it back to Wednesday. The idea did work pretty good with a flaw. Works fine as long as the Date in the table is greater than DOW 3 or 4. Any suggestion?
SELECT DateAdd(wk, DateDiff(wk, 0, Recons_Sales_Details.Recons_Date), 0) + 2
You need to look into SET DATEFIRST to do this:
SET DATEFIRST 4 --4 is Thursday week start
SQL Fiddle Demo

SQL Server 2005: arithmetic with dates

I would like to write a simple SELECT statement in SQL Server 2005 which does the following computation with date arithmetic:
Starting from the present date (this means getdate()), determine the previous Monday, and then subtract 70 days from that Monday, showing in output the resulting date.
How could I achieve this?
My difficulty is mainly to determine the previous Monday.
Of course, if getdate() is Monday, the previous Monday is getdate()
Thank you in advance for your kind help.
UltraCommit
EDIT: Please note that in Italy the first day of the week is Monday and not Sunday, so if the input is Sunday, July 29th, 2012, the output has to be 23rd July, and not 30th July.
This will retrieve monday for the current week
Select DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
and then you need to subtract 70 from the above day
SELECT Dateadd(DAY,-70,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
Edit : Please go through the answer posted in SO
Monday is displayed as Current Week because DATEFIRST which indicates the 1st day of the week is set to monday .In order to set it to Sunday ,you need to change the setting to Sunday
Set DATEFIRST 7
Else as suggested in the above SO link ,you need to change your code
DECLARE #dt DATE = '1905-01-01';
SELECT [start_of_week] = DATEADD(WEEK, DATEDIFF(WEEK, #dt, CURRENT_TIMESTAMP), #dt);
To get last monday look at
http://blog.sqlauthority.com/2007/08/20/sql-server-find-monday-of-the-current-week/
This should get you started. It will find the past Monday for the current week.
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) MondayOfCurrentWeek
To substract 70 days, just add -70 to the end:
SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)-70 as SomeMondayInHistory

Resources