I want to get two days before from current date and that date should not be Saturday and Sunday today is 1st July 2021 so i need to get data for 30th jun 2021 and 29th jun 2021 and that day should not be Saturday and Sunday.
Use a CASE expression:
SELECT CASE DATEPART(dw, GETDATE())
WHEN 2 THEN DATEADD(day, -3, GETDATE()) -- Monday becomes Friday
WHEN 3 THEN DATEADD(day, -4, GETDATE()) -- Tuesday becomes Friday
ELSE DATEADD(day, -2, GETDATE()) -- otherwise roll back 2 days
END;
The above answer assumes that server settings place Sunday as day 1, Monday as day 2, ..., and Saturday as day 7. Without this assumption my answer might be worthless.
Related
I have a rolling date with associated hours. I want to sum the hours over my defined 7-day week range of Saturday to Friday.
So I need to define any date as a week beginning Saturday, week ending Friday and sum over this range.
The table is in the form of:
Date
Day
Hours
2021-06-12
Saturday
3
2021-06-18
Friday
3
2021-06-21
Monday
1
2021-06-22
Tuesday
2
Grouping the above table into Saturday-Friday Week sums.
Saturday
Friday
Total_Hours
2021-06-12
2021-06-18
6
2021-06-19
2021-06-25
3
You can use date functions to get for each date (other than Saturday) the previous Saturday which is the start of the week it belongs too and group by that:
WITH cte AS (
SELECT *, DATEADD(day, -DATEPART(dw, Date) % 7, Date) Saturday
FROM tablename
)
SELECT Saturday,
DATEADD(day, 6, Saturday) Friday,
SUM(Hours) Total_Hours
FROM cte
GROUP BY Saturday
See the demo.
This code works in SQL Server but similar logic and functions can be used for other databases.
You don't have to tie this to datepart() or datename(), which in turn depends on DATEFIRST (alas!).
Instead, you can use date arithmetic. This in turn depends on the fact that 0 date is a Monday. But that is not configurable (as far as I know).
So:
select v.week as saturday, dateadd(day, 6, v.week) as friday,
sum(hours)
from t cross apply
(values (dateadd(day, 7*datediff(week, 0, dateadd(day, 1, t.date)) - 2, 0))
) v(week)
group by v.week
order by v.week;
Here is a db<>fiddle.
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().
I would like to display Monday as the first day of the week and Sunday the last day of the week.
I am using for this report the Report server.
my query:
Select
Datum,
Sum(Prod) as Prod
FROM (
Select
intervaldate as Datum,
Sum(case when TabName = 'Produzierte Dosen' then DisplayUnits else 0 end) as Prod
from vwOeeIntervalCount
where
IntervalDateWeek >= dateadd(wk, datediff(wk, 0, getdate()) - 1, 0)
and IntervalDateWeek < dateadd(wk, datediff(wk, 0, getdate()), 0)
and IntervalDate >= dateadd(day,datediff(day,0,GETDATE())-6,0)
AND IntervalDate < dateadd(day,datediff(day,0,GETDATE()),0)
and CalculationName = 'Packaging'
group by intervaldate
)c
group by Datum
Here the result:
Date |Prod
2018-02-25 00:00:00.000 |1836528
2018-02-26 00:00:00.000 |8131127,99999999
EDIT:
Sorry here is my question.
I would like to display the Monday as the first day of the week. My query displays the Sunday as the first day of the week.
How can I do that?
By default MS SQL Server has configured sunday as first day of a week.
You can set the server config for the first day of a week to any day with the following command:
SET DATEFIRST { number | #number_var }
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
Source: https://learn.microsoft.com/en-us/sql/t-sql/statements/set-datefirst-transact-sql
you can use this to the current week:
/*-- Week Start
--1->Sunday --2->Monday....*/
SELECT CAST(DATEADD(dd, -(DATEPART(dw, GETDATE()))+2, GETDATE()) AS DATE) [WeekStart],
CAST(DATEADD(dd, 8-(DATEPART(dw, GETDATE())), GETDATE()) AS DATE) [WeekEnd]
/*-- Week End
--7->Saturday --8-> Sunday...*/
Running this today will produce this result:
WeekStart WeekEnd
---------- ----------
2018-02-26 2018-03-04
if you have dates in your table, you can use them instead of the GETDATE()
I want to pull all the data for the week to the previous day.
for example:
if the date was Monday 2016-05-09. I would like the previous Monday 2016-05-02 to Sunday 2016-05-08.
If the date was Tuesday 2016-05-10. I would like the previous Monday 2016-05-09 to Monday 2016-05-09.
I have tried using with no joy.
and (ShiftDate Between DATEADD(wk, 0, DATEADD(DAY, (1-DATEPART(WEEKDAY, GETDATE()-1)), DATEDIFF(dd, 0, GETDATE())))and GETDATE()-1)
You should use this:
and (ShiftDate Between (GETDATE() - 1 - (DATEPART(weekday, GETDATE() -1) + 7 - 2) % 7)
and (GETDATE()-1)
)
Because Sunday is 1 in weekday so you should add 7 then module by 7 to get the different from nearest Monday (2 in weekday) to the previous day.
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.