?
Solved

sql get the end date

Posted on 2015-02-02
3
Medium Priority
?
305 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 51

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 600 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 1400 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

762 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