Solved

sql get the end date

Posted on 2015-02-02
3
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

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 50

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 49

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

696 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