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.
Related
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
I need to print the last sunday of the year from which adding 7 to it will give me all the sundays of next year.
I have the code to print all sundays for a particular year if i have a start date but i need the user to put the year so that last sunday of the previous year will be generated and 7 will be added to get first sunday of that year and so on till it reaches last sunday of next year
For example input year is 2017 it will check the last sunday of 2016 and add 7 to it to get first sunday of 2017 which is 1-1-2017 and it will go on printing all sundays till it reaches 31st december 2017
The function next_day() takes two arguments: a date and the name of a day of the week. It returns the closest "next" day (following the date argument) that matches the given day of the week. So the result is between one and seven days forward. (If you want 'Tuesday' and the input date is a Tuesday, the function returns the date seven days later.)
If you want the last Sunday of a year, it will be between Dec. 25 and Dec. 31. So if you call the next_day() function with the arguments Dec. 24 (!!) and 'Sunday' you'll get what you want.
The result will have the same time-of-day as the date argument, so if you give a date without a time-of-day, so will be the output (which is probably what you want). So:
select next_day(date '2016-12-24', 'Sunday') from dual;
NEXT_DAY(D
----------
2016-12-25
Added: If you take an input from your user, as a bind variable, you can do something like this:
select next_day(to_date(:input_year - 1 || '-12-24', 'yyyy-mm-dd'), 'Sunday') from dual;
If you provide 2017 as input (whatever mechanism your interface has for bind variables), the output will be 2016-12-25 (in DATE data type, so don't ask "in what format" - dates don't have a format!)
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
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.
Could some one please explain this to me as I am a touch confused as to why this is happening? Basically what I would like to know is why there is a difference between Sundays date and every other day of this week in weeks from the year 0. If that makes sense!
I tried setting the date first but this had no effect. Surely Monday – Sunday of this week should all have the same difference in weeks from the year zero?
Set Datefirst 1
Select DateName(dw,0) --Monday
Select DateDiff(week, 0, '20091109')--Monday: Difference 5732
Select DateDiff(week, 0, '20091114')--Saturday: Difference 5732
Select DateDiff(week, 0, '20091115')--Sunday: Difference 5733
What makes this even more bizarre is if you take the same two dates and date diff them you get one week for the one and 6 days for the other. Am I missing something here?
Select DateDiff(dd,'20091109','20091115')--6 Days difference
Select DateDiff(ww,'20091109','20091115')--1 Week difference
I am using SQL Server 2005
Sunday is considered the first day of the week in a number of countries.
All the datediff functions do is count the number of date boundarys between the two datetime arguments passed to it. So if a week is defined to start Sunday, then the week boundary is midnight Sunday Morning.
So, From 11:59 Saturday night to 00:01 am Sunday Morning, will be the same datediff(week, x, y) as from 00:01 Sunday to 11:59 PM Saturday Night - 13 days later.
x ------------------------------x ==> 1 week diff
| Su M T W T F S | Su M T W T F S |
| | |
x-x ==> Also 1 week diff
I think the problem you are facing is the boundary dates. Are they inclusive?
Select DateDiff(dd,'20091109','20091115')--6 Days difference
Select DateDiff(dd,'20091109','20091116')--7 Days difference
DateDiff - Returns the number of date and time boundaries crossed between two specified dates. (Books Online Definition)
If you Set Datefirst = 1, Monday is the first day of the week, and the week calculations would use MOD 7 for number of days in the week and /7 for week count, in which sunday would be used as 1, and the others FLOORed to 0.