Solved

sql get the end date

Posted on 2015-02-02
3
266 Views
Last Modified: 2015-02-03
I am trying to get the start and end date of the week. As a start date I seems to be manage to get the date, but have difficulties getting the end date. So my question is How would I get the end date?

Select WeekStart =
  convert(varchar(10),dateadd(week, datediff(week, 0, SU.sessionStart), 0),105)
  ,CountID = Count(S.sessionKey)
FROM SessionUnit SU
Inner join Session S on S.sessionKey = SU.sessionkey
inner join product P on p.productKey = S.productKey
CROSS APPLY (
    SELECT item AS SPLITLOCATIONKEY
    from DelimitedSplit8K(S.locationKeyList, ',')
)as ca
INNER JOIN location AS L2 ON CA.SplitLocationKey = L2.LocationKey AND (L2.locationKey in (225,228,260) OR S.locationKey in (225,228,260))
WHERE  su.sessionStart >= DateAdd(day,45,dateadd(day, datediff(day, 0, getdate()), 0))
AND su.sessionStart <  DateAdd(day,91,dateadd(day, datediff(day, 0, getdate()), 0))
AND datediff(day, 0, su.sessionStart) % 7 not in (5, 6) --always works regardless of date settings
AND p.productKey = 2  and SU.instructorKey = 42
GROUP BY
 dateadd(week, datediff(week, 0, SU.sessionStart), 0)
0
Comment
Question by:erikTsomik
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40585588
it depends on what exactly you define as "start" and "end date", but usually it should be +6 of the start date...
as sessionStart is usually not only date but also time, it's a +7, with:

start date starting on 00:00:00 and being included (>=), the end date is + 7 days , but excluded ( < )

say we start today (tuesday), 03/02/2015 00:00:00, the end date of +7 is 10/02/2015 00:00;00, but using <, it will work exactly as you want.

hope this helps
0
 
LVL 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 150 total points
ID: 40585761
You can adapt this code and use it for your issue (play with @MyDate variable so you can try as many options you want):
DECLARE @MyDate SMALLDATETIME

SET @MyDate= GETDATE()

SELECT DATEADD(day,-(DATEPART(weekday, @MyDate)-1),@MyDate) BOW, DATEADD(day,7-DATEPART(weekday, @MyDate),@MyDate) EOW

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 350 total points
ID: 40585884
As Guy mentions often you take the start day of a week and add 6 (this is so us humans don't get confused)
e.g.

Monday 2 Feb 2015 to Mon 9 Feb 2015 = 1 week BUT we often label this as:

WeekStart    WeeEnding
2015-02-02  2015-02-08

OR, if you are excluding Saturdays and Sundays (which appears to be the case, then a 5 day week would start Mon 2 feb and end Fri 6 Feb (add 4 days instead of 6)

WeekStart    WeeEnding
2015-02-02  2015-02-06

If you want 6 days try the following (or change the 6 to 4)
SELECT
      CONVERT(varchar(10),                DATEADD(WEEK, DATEDIFF(WEEK, 0, SU.sessionStart), 0)  , 105) AS WEEKSTART
      CONVERT(varchar(10), DATEADD(DAY,6, DATEADD(WEEK, DATEDIFF(WEEK, 0, SU.sessionStart), 0) ), 105) AS WEEKENDING
    , COUNTID = COUNT(S.sessionKey)
FROM SessionUnit SU
      INNER JOIN Session S ON S.sessionKey = SU.sessionkey
      INNER JOIN product P ON P.productKey = S.productKey
      CROSS APPLY (
            SELECT
                  item AS SPLITLOCATIONKEY
            FROM DelimitedSplit8K(S.locationKeyList, ',')
      ) AS CA
      INNER JOIN location AS L2 ON CA.SplitLocationKey = L2.LocationKey
      AND (L2.locationKey IN (225, 228, 260)
      OR S.locationKey IN (225, 228, 260))
WHERE SU.sessionStart >= DATEADD(DAY, 45, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))
AND SU.sessionStart < DATEADD(DAY, 91, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))
AND DATEDIFF(DAY, 0, SU.sessionStart) % 7 NOT IN (5, 6) --always works regardless of date settings
AND P.productKey = 2
AND SU.instructorKey = 42
GROUP BY
                     DATEADD(WEEK, DATEDIFF(WEEK, 0, SU.sessionStart), 0)
    , DATEADD(DAY,6, DATEADD(WEEK, DATEDIFF(WEEK, 0, SU.sessionStart), 0) )
;

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Better way to make a query with date filter. 5 27
Passing Parameter to Stored Procedure 4 24
SQL Query assistance 16 27
syntax sql error 2 14
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question