SQL - don't include current week's data - sql

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.

Related

SQL Server - Get calander month begining and end

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().

Select based on the next business day in a calendar table

I have a calendar table (Calendar_Date, Is_Business_Day) already filled in.
I have already managed to do a SELECT on this basis :
If today is before the 3rd day of the current month, select all the days before this day until the last day of the penultimate month
For example : Today is 2018-05-02, this is my output :
Calendar_Date | Is_Business_Day
2000-01-01 | 0
... |
2018-03-29 | 1
2018-03-30 | 1
2018-03-31 | 0
If today is after the 3rd day of the current month, select all the days before this day until the last day of the last month.
For example : Tomorrow, 2018-05-03 this will be my output :
Calendar_Date | Is_Business_Day
2000-01-01 | 0
... |
2018-04-28 | 0
2018-04-29 | 0
2018-04-30 | 1
This is my query :
SELECT Calendar_Date, Is_Business_Day
FROM Calendar_Table
WHERE (Calendar_Date <= (CASE WHEN DATEPART(day, GETDATE()) >= 3 THEN EOMONTH(DATEADD(MONTH, - 1, GETDATE())) ELSE EOMONTH(DATEADD(MONTH, - 2, GETDATE())) END))
This is working perfectly, but what i would like it to do now is to switch after the first business day after the 3rd day of the month, instead of switching after the 3rd day of the month.
How can I use the information about business days in my calendar table to do this?
I think following query should work.
;WITH CTE AS
(
SELECT Calendar_Date, Is_Business_Day
FROM Calendar_Table
WHERE (Calendar_Date <= (CASE WHEN DATEPART(day, GETDATE()) >= 3
THEN EOMONTH(DATEADD(MONTH, - 1, GETDATE()))
ELSE EOMONTH(DATEADD(MONTH, - 2, GETDATE())) END))
)
SELECT * FROM CTE
WHERE Calendar_Date >= (SELECT MIN(Calendar_Date) FROM CTE WHERE Is_Business_Day=1)

SQL: First day of week Monday

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()

How do I exclude SQL data that is in current Week of Year

Currently I have some SQL which works well, except that I am continuously having to remove the first line of the results, as it only contains (at this point, being Monday 17th) data for part of the week, and being grouped by this field it is showing 'fake data'
Here is the current code:
SELECT
YEAR(submitted) YEAR,
COUNT(request) Total_Requests,
DATEPART( wk, submitted) WEEK
FROM
wv_external_statistics
WHERE
userid <> 'anonymous'
GROUP BY
YEAR(submitted),
DATEPART( wk, submitted)
Here is some sample data:
YEAR | Total_Requests | WEEK
2017 | 361 | 28
2017 | 2486 | 27
2017 | 2860 | 26
2017 | 4521 | 25
2016 | 2600 | 52
2016 | 3028 | 51
....
As you can see the top row is the current week, and as we are only at the first day of the week the data is not complete, so I want to exclude this row from my results... I just tried the below, and immediately zero rows were found, so I am clearly doing something silly, which I am hoping someone can point out?
DATEPART( wk, submitted) <= DATEPART( wk, submitted)-1
NOTE: I need to keep all the data from the year 2016, even though it's week number will be greater than this week, the year will be from previous years.
Cheers
If you want to remove the current week. Why not just throw this in the where clause...
Where
Datepart( wk, submitted) != datepart(wk,getdate())
SELECT YEAR(SubmittedDate) As [YEAR]
,COUNT(Request) As Total_Requests
,DATEPART(WEEK, SubmittedDate) As [WEEK]
FROM wv_external_statistics
WHERE UserID <> 'Anonymous' Or ((DATEPART(WEEK, SubmittedDate) -
DATEPART(WEEK, GETDATE()) = 0) And (YEAR(SubmittedDate) - YEAR(GETDATE()) = 0))
GROUP BY YEAR(SubmittedDate),DATEPART(WEEK, SubmittedDate);
You need to specify having condition (well, should admit its long, fuzzy and maybe not optimal)
SELECT
YEAR(submitted) YEAR,
COUNT(request) Total_Requests,
DATEPART( wk, submitted) WEEK
FROM
wv_external_statistics
WHERE
userid <> 'anonymous'
GROUP BY
YEAR(submitted),
DATEPART( wk, submitted)
having year(submitted) * 100 + datepart(wk, submitted) < (select max(year(submitted) * 100 + datepart(wk, submitted)) from wv_external_statistics)
because your condition (DATEPART( wk, submitted) <= DATEPART( wk, submitted)-1) is always false for each row in the query

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.