set Monday is first day of week - sql

I'm using Microsoft sql server and in the sql server by default first day of week is Sunday but I need to set it Monday is the first day of week.

This sets the first day of the week to Monday
SET DATEFIRST 1;

You can use
SET DATEFIRST { number }
number
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
Here the link to the official docs:
https://learn.microsoft.com/en-US/sql/t-sql/statements/set-datefirst-transact-sql?view=sql-server-2017

You can use SET DATEFIRST like following.
SET DATEFIRST 1
You can read more about this here and here

Use this command :
SET DATEFIRST 1;
See this post

Related

SQL: Despite using 'SET DATEFIRST 1', why is function to calculate week commencing date picking up the preceding Sunday date? [duplicate]

I am running SQL Server 2008 on a machine with regional settings that have Monday as the first day of the week. If I create a computed column in a table to compute the day of week for a date field then I get 2 for a Monday date instead of 1.
Is there some property for the table or database or server that I need to set ?
The first day of the week is based on your language settings of the server. The default setting for us_english is 7 (Sunday)
You can find the current first day of the week by using SELECT ##DATEFIRST
However, you can use DATEFIRST for this. Just put it at the top of your query
SET DATEFIRST 1; this sets Monday to the first day of the week for the current connection.
http://technet.microsoft.com/en-us/library/ms181598.aspx
You can use DATEPART(dw, GETDATE()) but be aware that the result will rely on SQL server setting ##DATEFIRST value which is the first day of week setting (In Europe default value 7 which is Sunday).
Alternative way is to explicitly specify the first day of week value as parameter and avoid depending on ##DATEFIRST setting. You can use the following formula to achieve that when need it:
(DATEPART(dw, GETDATE()) + ##DATEFIRST + 6 - #WeekStartDay) % 7 + 1
where #WeekStartDay is the first day of the week you want for your system (from 1 to 7 which means from Monday to Sunday).
I have wrapped it into below function so we can reuse it easily:
CREATE FUNCTION [dbo].[GetDayInWeek](#InputDateTime DATETIME, #WeekStartDay INT)
RETURNS INT
AS
BEGIN
--Note: #WeekStartDay is number from [1 - 7] which is from Monday to Sunday
RETURN (DATEPART(dw, #InputDateTime) + ##DATEFIRST + 6 - #WeekStartDay) % 7 + 1
END
Example usage:
GetDayInWeek('2019-02-04 00:00:00', 1)
It is equivalent to following (but independent to DATEFIRST setting):
SET DATEFIRST 1
DATEPART(dw, '2019-02-04 00:00:00')
Just set in query
SET DATEFIRST 1;
Value
First day of the week is
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 (default, U.S. English) Sunday
A horrible situation....Say you are using a shared server and apps can get moved around to different servers based on loads and for some reason one server was setup with a different firstDate for another application....and you are returning the week day in a table function (ie can't set firstdate in this function). This will always guarantee Monday as the first day.
CREATE FUNCTION fnGetMondayWD(#WD INT)
RETURNS INT
AS
BEGIN
/* think of 1 to 7 as a clock that can rotate forwards or backwards */
DECLARE #OFFSET INT, #calc INT;
SET #offset = ##DATEFIRST + #WD - 1;--Monday DateFirst
SET #calc = IIF(#offset > 7, #offset - 7, #offset); -- could be #offset % 7 (less readable more efficient)
RETURN #calc;
END;
go
-- Test Cases
SET datefirst 7
select dbo.fnGetMondayWD(2) Mon,
dbo.fnGetMondayWD(3)Tue,
dbo.fnGetMondayWD(4)Wed,
dbo.fnGetMondayWD(5)Thur,
dbo.fnGetMondayWD(6)Fri,
dbo.fnGetMondayWD(7)Sat,
dbo.fnGetMondayWD(1)Sun
SET datefirst 6
select dbo.fnGetMondayWD(3) Mon,
dbo.fnGetMondayWD(4)Tue,
dbo.fnGetMondayWD(5)Wed,
dbo.fnGetMondayWD(6)Thur,
dbo.fnGetMondayWD(7)Fri,
dbo.fnGetMondayWD(1)Sat,
dbo.fnGetMondayWD(2)Sun
SET datefirst 5
select dbo.fnGetMondayWD(4) Mon,
dbo.fnGetMondayWD(5)Tue,
dbo.fnGetMondayWD(6)Wed,
dbo.fnGetMondayWD(7)Thur,
dbo.fnGetMondayWD(1)Fri,
dbo.fnGetMondayWD(2)Sat,
dbo.fnGetMondayWD(3)Sun
SET datefirst 1
select dbo.fnGetMondayWD(1) Mon,
dbo.fnGetMondayWD(2)Tue,
dbo.fnGetMondayWD(3)Wed,
dbo.fnGetMondayWD(4)Thur,
dbo.fnGetMondayWD(5)Fri,
dbo.fnGetMondayWD(6)Sat,
dbo.fnGetMondayWD(7)Sun

Get Work week or Number of Weeks T-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

How to properly use SET DATEFIRST function in SQL Server?

Trying to set the date as Monday and then order the results by the 'day of the week' starting with Monday. Unfortunately, it seems as if the DATEFIRST function doesn't do anything and my output keeps on thinking Friday is the first day of the week.
SET DATEFIRST 1;
SELECT FirstName, LastName, HireDate, datename(dw,HireDate) AS 'Day of the Week'
FROM Faculty
ORDER BY datename(dw,HireDate);
The code above produces the following output:
Lynda Baker 1989-09-15 Friday
Tim Stewart 2000-09-15 Friday
John Puckett 1989-09-15 Friday
Maria Lynn Kessler 2003-09-15 Monday
Leo Dubray 2001-09-15 Saturday
Jamie Zipay 2001-01-07 Sunday
Michele Malott 2005-09-15 Thursday
Robin Schwartz 1999-09-15 Wednesday
Anyone know how I can use DATEFIRST or any other function that will allow me to sort the day of the week starting with Monday?
You need to order by DATEPART not DATENAME.
DATEPART returns an integer, dependant on the DATEFIRST setting. DATENAME returns a string with the day of the week.
The string 'Monday' will always come after 'Friday' as it is alphabetically sorted.

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.

SELECT following Friday date

I need to select next friday from current date.
For example:
Today SELECT
Monday 6/24/2013 6/28/2013
Wednesday 6/26/2013 6/28/2013
Friday 6/28/2013 6/28/2013
Saturday 6/29/2013 7/5/2013
I found a few solutions close to what I want but all they do is select fridat of the day's week.
I need to use this date as a Default Value in the table column. When a user inserts a record, this column should automatically be set to the folloing Friday. Say, instead of getdate() in the 'Default Value or Binding', I need to put this statement that selects following Friday.
select DATEADD(day,6-datepart(weekday,the_dt),the_dt)
+ Case when datepart(weekday,the_dt) = 7 then 7 else 0 end
from table