SQL Server - Get calander month begining and end - sql

I've tried searching but cannot find anything.
I am trying to get the first and last date of the calander month.
So for example the calander month for January 2020 actually starts on December 30th 2019 and ends on February 2nd 2020. (Week 1 - 5)
|---------------------|-------------------|-------------------|
| Week number | From Date | To Date |
|---------------------|-------------------|-------------------|
| Week 01 | December 30, 2019 | January 5, 2020 |
|---------------------|-------------------|-------------------|
| Week 05 | January 27, 2020 | February 2, 2020 |
|---------------------|-------------------|-------------------|
Using this website to get week numbers
Is this possible?
Many thanks.

If you are using MSSQL-2012 or onwards.
DECLARE #DATE DATETIME='29-JAN-2020'
SELECT DATEADD(DAY, 2 - CASE WHEN DATEPART(WEEKDAY, #DATE-DAY(#DATE)+1)=1 THEN 8 ELSE DATEPART(WEEKDAY, #DATE-DAY(#DATE)+1) END, CAST( #DATE-DAY(#DATE)+1 AS DATE)) [MONTH_START_DATE],
DATEADD(DAY, 8 - CASE WHEN DATEPART(WEEKDAY, EOMONTH(#DATE))=1 THEN 8 ELSE DATEPART(WEEKDAY, EOMONTH(#DATE)) END , CAST(EOMONTH(#DATE) AS DATE)) [MONTH_END_DATE];
You can try on below link:-
https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=9747ea25d0d0bc343be8dbcc90803303

You can use this logic:
select convert(date, dateadd(day, 1 - day(getdate()), getdate())) as month_first,
dateadd(day, 1, eomonth(getdate(), -1)) as alternative_month_first,
eomonth(getdate()) as month_last
Of course, you would use whatever date you wanted instead of getdate().

Related

-SQL - Get date up to present date and year

i'm trying to get the date from the start up to present date and year.
For example:
Date Today Jan 31 , 2020
the result must be
jan 1 - 31 2020 ,
Another example: if the date is june 30, 2020
the result must be
jan 1 - june 30, 2020
Can you help me how to do it ? , i'm planning to use BETWEEN
DECLARE #date date = ' 2019-05-01'
SELECT EOMONTH(#date), DATEADD(yy, DATEDIFF(yy, 0, #date), 0)

SQL - don't include current week's data

I have some weekly code that when run I don't want it to include the current week's data. The dates are slightly odd because it is a financial calendar.
| Date | Week | Year|
---------------------------
|30-12-2017 | 52 | 2017|
|31-12-2017 | 1 | 2018|
|01-01-2018 | 1 | 2018|
I've looked at
where week = datepart(ww, date) -1
and year = datepart(yyyy, date)
but this doesn't work at the end of the year when the year ticks over and week 1 minus 1 does not equal week 52
Compare dates. dateadd(ww, datediff(ww,0,getdate()), 0) will get first day of week. So your query should look like
where
dateColumn < dateadd(ww, datediff(ww,0,getdate()), 0)
You could use:
where week = datepart(ww, dateadd(week, -1, getdate()) and
year = datepart(yyyy, dateadd(week, -1, getdate())
I'm not sure what date is in your code. getdate() is for the current date. Of course, any other date could be substituted to get the previous week.
I think you want such clause:
where (year < datepart(yyyy, getdate())) or
(week < datepart(ww, getdate()) and
year = datepart(yyyy, getdate()))
The logic is: take all records where year is less than current year (data from past years), also take records from current year, but where week number is less than current week.

How to get month value using week value sql server

I want to get month value using week no.
I have week numbers stored in a table with year value.
How to query database to get month value using that week value.
I am using SQL
You can try this:
SELECT DATEPART(m,DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + CAST(t.year as VARCHAR(4))) + (t.week-1), 6))
It depends on how you're classing your week numbers, For example, if we assume that week numbers start on a Monday then we'd have to say that week 1 in 2016 actually started on Monday 28th of December 2015 and finished on Sunday 3rd January 2016. If this is how your week numbers are set up then you can use the method below
Sample Data;
CREATE TABLE #DateTable (WeekNum int, YearNum int)
INSERT INTO #DateTable (WeekNum, YearNum)
VALUES
(1,2016)
,(2,2016)
,(3,2016)
,(4,2016)
,(5,2016)
,(6,2016)
,(7,2016)
We will then cast the week and year into a date, then convert this to a month;
SELECT
WeekNum
,YearNum
,DATEADD(wk, DATEDIFF(wk, 7, '1/1/' + CONVERT(varchar(4),YearNum)) + (WeekNum-1), 7) AS WeekStart
,DATEPART(mm,DATEADD(wk, DATEDIFF(wk, 7, '1/1/' + CONVERT(varchar(4),YearNum)) + (WeekNum-1), 7)) MonthNum
(Edit: updated as source is int)
Gives these results;
WeekNum YearNum WeekStart MonthNum
1 2016 2015-12-28 00:00:00.000 12
2 2016 2016-01-04 00:00:00.000 1
3 2016 2016-01-11 00:00:00.000 1
4 2016 2016-01-18 00:00:00.000 1
5 2016 2016-01-25 00:00:00.000 1
6 2016 2016-02-01 00:00:00.000 2
7 2016 2016-02-08 00:00:00.000 2
You can't go from week number to month because weeks can occur in two different months. For example the 31st Jan 2016 and 1st Feb 2016 are both in week 6.
SELECT DATEPART(WEEK, '2016-01-31')
SELECT DATEPART(WEEK, '2016-02-01')
You can try the query below:
SELECT
[Week],
[Year],
'Output-Month' = MONTH(DATEADD(WEEK, [Week], DATEADD(WEEK, DATEDIFF(WEEK, '19050101', '01/01/' + CAST([Year] AS VARCHAR(4))), '19050101')))
FROM YourTable
1st is to get the 1st day of the year using this:
DATEADD(WEEK, DATEDIFF(WEEK, '19050101', '01/01/' + CAST([Year] AS VARCHAR(4))), '19050101')
2nd is to add your number of week using this:
DATEADD(WEEK, [Week], 'From 1st result')
Last is getting the number of Month using the MONTH function.

Date Variable for last working day in UK?

Working on UK based project.
I put a lovely variable in to give me a start date and end date of the last working day for a Monday to Friday . Unfortuantely I wanted the start date time to be the start of the day and the end date to be the end of the day . What change can I make to make this happen ?
SET #Startdate =
( CASE
WHEN DATEPART(WEEKDAY, #Startdate) = 7
THEN DATEADD(DAY, -2, #Startdate)
WHEN DATEPART(WEEKDAY, #Startdate) = 1
THEN DATEADD(DAY, (-2 - DATEPART(WEEKDAY, #Startdate)), #Startdate)
ELSE
DATEADD(DAY, -1, #Startdate)
END )
SET #Enddate =
CASE
WHEN DATEPART(WEEKDAY, #Enddate) = 7
THEN DATEADD(DAY, -2, #Enddate)
WHEN DATEPART(WEEKDAY, #Enddate) = 1
THEN DATEADD(DAY, (-2 - DATEPART(WEEKDAY, #Enddate)), #Enddate)
ELSE
DATEADD(DAY, -1, #Enddate)
END
print #Startdate
print #Enddate
Results :
Feb 26 2013 12:00AM
Feb 26 2013 12:00AM
I need Feb 26 2013 00:00 until Feb 27 2013 00:00, so it covers all day Tuesday (the last working day if run today).
This is a solved problem if you perform a cursory Google search (lots of good UDFs for this), but to use your code as is, just add one to the #Enddate calculation if you really do want 26th 0:00 to 27th 0:00 (or add just below 1 like Kenneth Fisher suggests if you want 23:59, which seems better).
Actually 12:00AM is 00:00, you would just need to convert it in another format if you want (http://msdn.microsoft.com/en-us/library/ms187928.aspx).
Just add one day to Feb 26 12:00AM and you'll get Feb 27 12:00AM which is Feb 27 00:00.

Get the week start date and week end date from week number

I have a query that counts member's wedding dates in the database.
SELECT
SUM(NumberOfBrides) AS [Wedding Count]
, DATEPART( wk, WeddingDate) AS [Week Number]
, DATEPART( year, WeddingDate) AS [Year]
FROM MemberWeddingDates
GROUP BY DATEPART(year, WeddingDate), DATEPART(wk, WeddingDate)
ORDER BY SUM(NumberOfBrides) DESC
How do I work out when the start and end of each week represented in the result set?
SELECT
SUM(NumberOfBrides) AS [Wedding Count]
, DATEPART(wk, WeddingDate) AS [Week Number]
, DATEPART(year, WeddingDate) AS [Year]
, ??? AS WeekStart
, ??? AS WeekEnd
FROM MemberWeddingDates
GROUP BY DATEPART(year, WeddingDate), DATEPART(wk, WeddingDate)
ORDER BY SUM(NumberOfBrides) DESC
You can find the day of week and do a date add on days to get the start and end dates..
DATEADD(dd, -(DATEPART(dw, WeddingDate)-1), WeddingDate) [WeekStart]
DATEADD(dd, 7-(DATEPART(dw, WeddingDate)), WeddingDate) [WeekEnd]
You probably also want to look at stripping off the time from the date as well though.
Here is a DATEFIRST agnostic solution:
SET DATEFIRST 4 /* or use any other weird value to test it */
DECLARE #d DATETIME
SET #d = GETDATE()
SELECT
#d ThatDate,
DATEADD(dd, 0 - (##DATEFIRST + 5 + DATEPART(dw, #d)) % 7, #d) Monday,
DATEADD(dd, 6 - (##DATEFIRST + 5 + DATEPART(dw, #d)) % 7, #d) Sunday
you can also use this:
SELECT DATEADD(day, DATEDIFF(day, 0, WeddingDate) /7*7, 0) AS weekstart,
DATEADD(day, DATEDIFF(day, 6, WeddingDate-1) /7*7 + 7, 6) AS WeekEnd
Here is another version. If your Scenario requires Saturday to be 1st day of Week and Friday to be last day of Week, the below code will handle that:
DECLARE #myDate DATE = GETDATE()
SELECT #myDate,
DATENAME(WEEKDAY,#myDate),
DATEADD(DD,-(CHOOSE(DATEPART(dw, #myDate), 1,2,3,4,5,6,0)),#myDate) AS WeekStartDate,
DATEADD(DD,7-CHOOSE(DATEPART(dw, #myDate), 2,3,4,5,6,7,1),#myDate) AS WeekEndDate
Expanding on #Tomalak's answer. The formula works for days other than Sunday and Monday but you need to use different values for where the 5 is. A way to arrive at the value you need is
Value Needed = 7 - (Value From Date First Documentation for Desired Day Of Week) - 1
here is a link to the document: https://msdn.microsoft.com/en-us/library/ms181598.aspx
And here is a table that lays it out for you.
| DATEFIRST VALUE | Formula Value | 7 - DATEFIRSTVALUE - 1
Monday | 1 | 5 | 7 - 1- 1 = 5
Tuesday | 2 | 4 | 7 - 2 - 1 = 4
Wednesday | 3 | 3 | 7 - 3 - 1 = 3
Thursday | 4 | 2 | 7 - 4 - 1 = 2
Friday | 5 | 1 | 7 - 5 - 1 = 1
Saturday | 6 | 0 | 7 - 6 - 1 = 0
Sunday | 7 | -1 | 7 - 7 - 1 = -1
But you don't have to remember that table and just the formula, and actually you could use a slightly different one too the main need is to use a value that will make the remainder the correct number of days.
Here is a working example:
DECLARE #MondayDateFirstValue INT = 1
DECLARE #FridayDateFirstValue INT = 5
DECLARE #TestDate DATE = GETDATE()
SET #MondayDateFirstValue = 7 - #MondayDateFirstValue - 1
SET #FridayDateFirstValue = 7 - #FridayDateFirstValue - 1
SET DATEFIRST 6 -- notice this is saturday
SELECT
DATEADD(DAY, 0 - (##DATEFIRST + #MondayDateFirstValue + DATEPART(dw,#TestDate)) % 7, #TestDate) as MondayStartOfWeek
,DATEADD(DAY, 6 - (##DATEFIRST + #MondayDateFirstValue + DATEPART(dw,#TestDate)) % 7, #TestDate) as MondayEndOfWeek
,DATEADD(DAY, 0 - (##DATEFIRST + #FridayDateFirstValue + DATEPART(dw,#TestDate)) % 7, #TestDate) as FridayStartOfWeek
,DATEADD(DAY, 6 - (##DATEFIRST + #FridayDateFirstValue + DATEPART(dw,#TestDate)) % 7, #TestDate) as FridayEndOfWeek
SET DATEFIRST 2 --notice this is tuesday
SELECT
DATEADD(DAY, 0 - (##DATEFIRST + #MondayDateFirstValue + DATEPART(dw,#TestDate)) % 7, #TestDate) as MondayStartOfWeek
,DATEADD(DAY, 6 - (##DATEFIRST + #MondayDateFirstValue + DATEPART(dw,#TestDate)) % 7, #TestDate) as MondayEndOfWeek
,DATEADD(DAY, 0 - (##DATEFIRST + #FridayDateFirstValue + DATEPART(dw,#TestDate)) % 7, #TestDate) as FridayStartOfWeek
,DATEADD(DAY, 6 - (##DATEFIRST + #FridayDateFirstValue + DATEPART(dw,#TestDate)) % 7, #TestDate) as FridayEndOfWeek
This method would be agnostic of the DATEFIRST Setting which is what I needed as I am building out a date dimension with multiple week methods included.
If sunday is considered as week start day, then here is the code
Declare #currentdate date = '18 Jun 2020'
select DATEADD(D, -(DATEPART(WEEKDAY, #currentdate) - 1), #currentdate)
select DATEADD(D, (7 - DATEPART(WEEKDAY, #currentdate)), #currentdate)
Below query will give data between start and end of current week
starting from sunday to saturday
SELECT DOB FROM PROFILE_INFO WHERE DAY(DOB) BETWEEN
DAY( CURRENT_DATE() - (SELECT DAYOFWEEK(CURRENT_DATE())-1))
AND
DAY((CURRENT_DATE()+(7 - (SELECT DAYOFWEEK(CURRENT_DATE())) ) ))
AND
MONTH(DOB)=MONTH(CURRENT_DATE())
Let us break the problem down to two parts:
1) Determine the day of week
The DATEPART(dw, ...) returns a number, 1...7, relative to DATEFIRST setting (docs). The following table summarizes the possible values:
##DATEFIRST
+------------------------------------+-----+-----+-----+-----+-----+-----+-----+-----+
| | 1 | 2 | 3 | 4 | 5 | 6 | 7 | DOW |
+------------------------------------+-----+-----+-----+-----+-----+-----+-----+-----+
| DATEPART(dw, /*Mon*/ '20010101') | 1 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
| DATEPART(dw, /*Tue*/ '20010102') | 2 | 1 | 7 | 6 | 5 | 4 | 3 | 2 |
| DATEPART(dw, /*Wed*/ '20010103') | 3 | 2 | 1 | 7 | 6 | 5 | 4 | 3 |
| DATEPART(dw, /*Thu*/ '20010104') | 4 | 3 | 2 | 1 | 7 | 6 | 5 | 4 |
| DATEPART(dw, /*Fri*/ '20010105') | 5 | 4 | 3 | 2 | 1 | 7 | 6 | 5 |
| DATEPART(dw, /*Sat*/ '20010106') | 6 | 5 | 4 | 3 | 2 | 1 | 7 | 6 |
| DATEPART(dw, /*Sun*/ '20010107') | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 7 |
+------------------------------------+-----+-----+-----+-----+-----+-----+-----+-----+
The last column contains the ideal day-of-week value for Monday to Sunday weeks*. By just looking at the chart we come up with the following equation:
(##DATEFIRST + DATEPART(dw, SomeDate) - 1 - 1) % 7 + 1
2) Calculate the Monday and Sunday for given date
This is trivial thanks to the day-of-week value. Here is an example:
WITH TestData(SomeDate) AS (
SELECT CAST('20001225' AS DATETIME) UNION ALL
SELECT CAST('20001226' AS DATETIME) UNION ALL
SELECT CAST('20001227' AS DATETIME) UNION ALL
SELECT CAST('20001228' AS DATETIME) UNION ALL
SELECT CAST('20001229' AS DATETIME) UNION ALL
SELECT CAST('20001230' AS DATETIME) UNION ALL
SELECT CAST('20001231' AS DATETIME) UNION ALL
SELECT CAST('20010101' AS DATETIME) UNION ALL
SELECT CAST('20010102' AS DATETIME) UNION ALL
SELECT CAST('20010103' AS DATETIME) UNION ALL
SELECT CAST('20010104' AS DATETIME) UNION ALL
SELECT CAST('20010105' AS DATETIME) UNION ALL
SELECT CAST('20010106' AS DATETIME) UNION ALL
SELECT CAST('20010107' AS DATETIME) UNION ALL
SELECT CAST('20010108' AS DATETIME) UNION ALL
SELECT CAST('20010109' AS DATETIME) UNION ALL
SELECT CAST('20010110' AS DATETIME) UNION ALL
SELECT CAST('20010111' AS DATETIME) UNION ALL
SELECT CAST('20010112' AS DATETIME) UNION ALL
SELECT CAST('20010113' AS DATETIME) UNION ALL
SELECT CAST('20010114' AS DATETIME)
), TestDataPlusDOW AS (
SELECT SomeDate, (##DATEFIRST + DATEPART(dw, SomeDate) - 1 - 1) % 7 + 1 AS DOW
FROM TestData
)
SELECT
FORMAT(SomeDate, 'ddd yyyy-MM-dd') AS SomeDate,
FORMAT(DATEADD(dd, -DOW + 1, SomeDate), 'ddd yyyy-MM-dd') AS [Monday],
FORMAT(DATEADD(dd, -DOW + 1 + 6, SomeDate), 'ddd yyyy-MM-dd') AS [Sunday]
FROM TestDataPlusDOW
Output:
+------------------+------------------+------------------+
| SomeDate | Monday | Sunday |
+------------------+------------------+------------------+
| Mon 2000-12-25 | Mon 2000-12-25 | Sun 2000-12-31 |
| Tue 2000-12-26 | Mon 2000-12-25 | Sun 2000-12-31 |
| Wed 2000-12-27 | Mon 2000-12-25 | Sun 2000-12-31 |
| Thu 2000-12-28 | Mon 2000-12-25 | Sun 2000-12-31 |
| Fri 2000-12-29 | Mon 2000-12-25 | Sun 2000-12-31 |
| Sat 2000-12-30 | Mon 2000-12-25 | Sun 2000-12-31 |
| Sun 2000-12-31 | Mon 2000-12-25 | Sun 2000-12-31 |
| Mon 2001-01-01 | Mon 2001-01-01 | Sun 2001-01-07 |
| Tue 2001-01-02 | Mon 2001-01-01 | Sun 2001-01-07 |
| Wed 2001-01-03 | Mon 2001-01-01 | Sun 2001-01-07 |
| Thu 2001-01-04 | Mon 2001-01-01 | Sun 2001-01-07 |
| Fri 2001-01-05 | Mon 2001-01-01 | Sun 2001-01-07 |
| Sat 2001-01-06 | Mon 2001-01-01 | Sun 2001-01-07 |
| Sun 2001-01-07 | Mon 2001-01-01 | Sun 2001-01-07 |
| Mon 2001-01-08 | Mon 2001-01-08 | Sun 2001-01-14 |
| Tue 2001-01-09 | Mon 2001-01-08 | Sun 2001-01-14 |
| Wed 2001-01-10 | Mon 2001-01-08 | Sun 2001-01-14 |
| Thu 2001-01-11 | Mon 2001-01-08 | Sun 2001-01-14 |
| Fri 2001-01-12 | Mon 2001-01-08 | Sun 2001-01-14 |
| Sat 2001-01-13 | Mon 2001-01-08 | Sun 2001-01-14 |
| Sun 2001-01-14 | Mon 2001-01-08 | Sun 2001-01-14 |
+------------------+------------------+------------------+
* For Sunday to Saturday weeks you need to adjust the equation just a little, like add 1 somewhere.
I just encounter a similar case with this one, but the solution here seems not helping me.
So I try to figure it out by myself. I work out the week start date only, week end date should be of similar logic.
Select
Sum(NumberOfBrides) As [Wedding Count],
DATEPART( wk, WeddingDate) as [Week Number],
DATEPART( year, WeddingDate) as [Year],
DATEADD(DAY, 1 - DATEPART(WEEKDAY, dateadd(wk, DATEPART( wk, WeddingDate)-1, DATEADD(yy,DATEPART( year, WeddingDate)-1900,0))), dateadd(wk, DATEPART( wk, WeddingDate)-1, DATEADD(yy,DATEPART( year, WeddingDate)-1900,0))) as [Week Start]
FROM MemberWeddingDates
Group By DATEPART( year, WeddingDate), DATEPART( wk, WeddingDate)
Order By Sum(NumberOfBrides) Desc
The most voted answer works fine except for the 1st week and last week of a year. For example, if the value of WeddingDate is '2016-01-01', the result will be 2015-12-27 and 2016-01-02, but the right answer is 2016-01-01 and 2016-01-02.
Try this:
Select
Sum(NumberOfBrides) As [Wedding Count],
DATEPART( wk, WeddingDate) as [Week Number],
DATEPART( year, WeddingDate) as [Year],
MAX(CASE WHEN DATEPART(WEEK, WeddingDate) = 1 THEN CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0) AS date) ELSE DATEADD(DAY, 7 * DATEPART(WEEK, WeddingDate), DATEADD(DAY, -(DATEPART(WEEKDAY, DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0)) + 6), DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0))) END) as WeekStart,
MAX(CASE WHEN DATEPART(WEEK, WeddingDate) = DATEPART(WEEK, DATEADD(DAY, -1, DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate) + 1, 0))) THEN DATEADD(DAY, -1, DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate) + 1, 0)) ELSE DATEADD(DAY, 7 * DATEPART(WEEK, WeddingDate) + 6, DATEADD(DAY, -(DATEPART(WEEKDAY, DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0)) + 6), DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0))) END) as WeekEnd
FROM MemberWeddingDates
Group By DATEPART( year, WeddingDate), DATEPART( wk, WeddingDate)
Order By Sum(NumberOfBrides) Desc;
The result looks like:
It works for all weeks, 1st or others.
Week Start & End Date From Date For Power BI Dax Formula
WeekStartDate = [DateColumn] - (WEEKDAY([DateColumn])-1)
WeekEndDate = [DateColumn] + (7-WEEKDAY([DateColumn]))
This is my solution
SET DATEFIRST 1; /* change to use a different datefirst */
DECLARE #date DATETIME
SET #date = CAST('2/6/2019' as date)
SELECT DATEADD(dd,0 - (DATEPART(dw, #date) - 1) ,#date) [dateFrom],
DATEADD(dd,6 - (DATEPART(dw, #date) - 1) ,#date) [dateTo]
Get Start Date & End Date by Custom Date
DECLARE #Date NVARCHAR(50)='05/19/2019'
SELECT
DATEADD(DAY,CASE WHEN DATEPART(WEEKDAY, #Date)=1 THEN -6 ELSE 2 - DATEPART(WEEKDAY, #Date) END, CAST(#Date AS DATE)) [Week_Start_Date]
,DATEADD(DAY,CASE WHEN DATEPART(WEEKDAY, #Date)=1 THEN 0 ELSE 8 - DATEPART(WEEKDAY, #Date) END, CAST(#Date AS DATE)) [Week_End_Date]
Another way to do it:
declare #week_number int = 6280 -- 2020-05-07
declare #start_weekday int = 0 -- Monday
declare #end_weekday int = 6 -- next Sunday
select
dateadd(week, #week_number, #start_weekday),
dateadd(week, #week_number, #end_weekday)
Explanation:
#week_number is the week number since the initial calendar date '1900-01-01'. It can be computed this way: select datediff(week, 0, #wedding_date) as week_number
#start_weekday for the week first day: 0 for Monday, -1 if Sunday
#end_weekday for the week last day: 6 for next Sunday, 5 if Saturday
dateadd(week, #week_number, #end_weekday): adds the given number of weeks and the given number of days into the initial calendar date '1900-01-01'
This doesn't come from me, but it got the job done regardless:
SELECT DATEADD(wk, -1, DATEADD(DAY, 1-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) --first day previous week
SELECT DATEADD(wk, 0, DATEADD(DAY, 1-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) --first day current week
SELECT DATEADD(wk, 1, DATEADD(DAY, 1-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) --first day next week
SELECT DATEADD(wk, 0, DATEADD(DAY, 0-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) --last day previous week
SELECT DATEADD(wk, 1, DATEADD(DAY, 0-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) --last day current week
SELECT DATEADD(wk, 2, DATEADD(DAY, 0-DATEPART(WEEKDAY, GETDATE()), DATEDIFF(dd, 0, GETDATE()))) --last day next week
I found it here.
I have a way other, It is select day Start and day End of Week Current:
DATEADD(d, -(DATEPART(dw, GETDATE()-2)), GETDATE()) is date time Start
and
DATEADD(day,7-(DATEPART(dw,GETDATE()-1)),GETDATE()) is date time End
Not sure how useful this is, but I ended up here from looking for a solution on Netezza SQL and couldn't find one on stack overflow.
For IBM netezza you would use something (for week start mon, week end sun) like:
select
next_day (WeddingDate, 'SUN') -6 as WeekStart,
next_day (WeddingDate, 'SUN') as WeekEnd
for Access Queries, you can use in the below format as a field
"FirstDayofWeek:IIf(IsDate([ForwardedForActionDate]),CDate(Format([ForwardedForActionDate],"dd/mm/yyyy"))-(Weekday([ForwardedForActionDate])-1))"
direct Calculation allowed..