[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

sql get the end date

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
erikTsomik
Asked:
erikTsomik
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
PortletPaulCommented:
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now