Solved

sql get the end date

Posted on 2015-02-02
3
256 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]
Comment Utility
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 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 150 total points
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now