How to properly use SET DATEFIRST function in SQL Server? - sql

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.

Related

set Monday is first day of week

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

How to query last Saturday and the one before in SQL?

I would like to query data, which is between some week days, as follows:
Last Saturday
The Sunday before it
The Saturday before that queried Sunday on 2.
The Sunday before that queried Saturday on 3.
The query would run automatically every Monday, so I would like to set a dynamic condition for it, so that it automatically picks that days, without depending on any day it will run on in the future.
So for example if today is Monday 01/08/2018:
would be 01/06/2018
would be 12/31/2017
would be 12/30/2017
would be 12/24/2017
I would like to set that conditions in the WHERE clause. For now I am querying it this way, with constant dates for example for last week data:
SELECT *
FROM thisTable
WHERE Date(operation_date) BETWEEN '2018-06-10' AND '2018-06-16'
DBMS: Amazon Redshift
The DATE_TRUNC Function is your friend. It truncates to a Monday.
SELECT
CURRENT_DATE AS today,
DATE_TRUNC('week', CURRENT_DATE) as most_recent_monday,
DATE_TRUNC('week', CURRENT_DATE) - 2 AS most_recent_saturday,
DATE_TRUNC('week', CURRENT_DATE) - 8 AS sunday_before_most_recent_saturday
Returns:
2018-06-21 | 2018-06-18 00:00:00 | 2018-06-16 00:00:00 | 2018-06-10 00:00:00
Note that it treats the date as midnight at the beginning of the day. So, you don't really want to query Sunday to Saturday. You actually want to query Sunday to Sunday (which really means midnight at the start of Sunday to midnight at the start of the next Sunday). This assumes your source date is a timestamp.
If your source date is purely a date, then you would want to use Sunday to Saturday.
If you want to query everything from "last week" (if your definition is Sunday to Sunday), and assuming a timestamp, use:
SELECT *
FROM thisTable
WHERE operation_date BETWEEN
-- Most recent Monday minus 8 days = Two Sundays ago
DATE_TRUNC('week', CURRENT_DATE) - 8 AND
-- Most recent Monday minus 1 day = Most recent Sunday
DATE_TRUNC('week', CURRENT_DATE) - 1
(Well, unless you're on a Sunday already, but that's your problem!)
If the date is a date, you'd have to adjust it a bit.
last Sat: date_trunc('week',getdate())-interval '2 day'
prev Sat: date_trunc('week',getdate())-interval '9 day'
this is for Monday-based week

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 format date column to get the day name in Oracle SQL?

I have a oracle db and there are two columns so i want to display day ex:(SUNDAY, MONDAY...) according to given date in db.
Table Name: TBL_HOLIDAY_MASTER:
Holiday_date Description
***********************************
22-NOV-15 Weekly Holiday
23-NOV-15 Working Day
24-NOV-15 Working Day
29-NOV-15 Weekly Holiday
30-NOV-15 Working Day
21-MAY-17 Weekly Holiday
18-AUG-19 Weekly Holiday
I want output Like:-
Holiday_date Description
*************************************
SUNDAY Weekly Holiday
MONDAY Working Day
TUESDAY Working Day
SUNDAY Weekly Holiday
MONDAY Working Day
SUNDAY Weekly Holiday
SUNDAY Weekly Holiday
You may achieve this with TO_CHAR function with DAY parameter, in your case it would be:
SELECT TO_CHAR(Holiday_date,'DAY') as Holiday_date, Description
FROM TBL_HOLIDAY_MASTER;
You need to use TO_CHAR and FMDAY format to get the day name. FM is required to remove the trailing blank spaces.
TO_CHAR(date_column, 'FMDAY', 'NLS_DATE_LANGUAGE=ENGLISH')
For example,
SQL> SELECT TO_CHAR(SYSDATE + LEVEL -1, 'FMDAY', 'NLS_DATE_LANGUAGE=ENGLISH') "DAYS"
2 FROM DUAL
3 CONNECT BY level <= 7;
DAYS
---------
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
7 rows selected.

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.