Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Incluse Saturday and Sunday in calculation


Incluse Saturday and Sunday in calculation

Author
Message
Lempster
Lempster
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2683 Visits: 1657
DaveDB (1/14/2014)

DATEPART(dw,Datestamp) IN (1,7)))


Just one thing to watch out for DaveDB, the code above relies on your instance having Sunday as the first day of the week. That might not be the case on every instance/database, particularly if the default language for the isntance is not us_english, so you might want to explicitly set it as such by running:
SET DATEFIRST 7


You can check what the first day of the week is set to by running:
SELECT @@DATEFIRST



Regards
Lempster
DaveDB
DaveDB
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 215
This comment is very insulting.

"Perhaps the reason for my request for additional material is becoming clearer now.

The fact that accomplished professionals, who would usually be able to answer this question while simultaneously juggling on a unicycle, are having to guess your requirements is proof enough."



All I asked was.... "How can you filter Saturday and Sunday out of a query". You do not need a result set for this particular question and if you do.... well.... then it's on you. I am not looking for trouble, but in all honesty, you seem more interested in playing gatekeeper to SSC than actually answering questions. Did you really not understand my question? MSDN Forums didn't seem to have an issue.

* shrug
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8966 Visits: 19020
DaveDB (1/24/2014)
This comment is very insulting.

"Perhaps the reason for my request for additional material is becoming clearer now.

The fact that accomplished professionals, who would usually be able to answer this question while simultaneously juggling on a unicycle, are having to guess your requirements is proof enough."



All I asked was.... "How can you filter Saturday and Sunday out of a query". You do not need a result set for this particular question and if you do.... well.... then it's on you. I am not looking for trouble, but in all honesty, you seem more interested in playing gatekeeper to SSC than actually answering questions. Did you really not understand my question? MSDN Forums didn't seem to have an issue.

* shrug


Nonsense. Here's a gift for you to show that neither Phil nor anyone else who has responded on this thread means any ill will:
CREATE FUNCTION [dbo].[IF_Calendar] 
(
@StartDate DATE,
@EndDate DATE,
@FirstWeekDay VARCHAR(10)
)
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
(

WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a CROSS JOIN E1 b), --10E+2 or 100 rows
E3(N) AS (SELECT 1 FROM E2 a CROSS JOIN E2 b), --10E+4 or 10,000 rows max

iTally AS ( -- generate sufficient rows to cover startdate to enddate inclusive
SELECT TOP(1+DATEDIFF(DAY,@StartDate,@EndDate))
rn = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))-1
FROM E3
)

-- Do some date arithmetic
SELECT
a.DateRange,
c.[Year],
c.[Month],
c.[DayOfMonth],
c.AbsWeekno,
c.[DayName],
d.Holiday
FROM iTally
CROSS APPLY (SELECT DateRange = DATEADD(day,rn,@StartDate)) a
CROSS APPLY (VALUES ('Tuesday',1),('Wednesday',2),('Thursday',3),('Friday',4),('Saturday',5),('Sunday',6),('Monday',7)
) b (FirstWeekDay, FirstWeekdayOffset)
CROSS APPLY (
SELECT
[Year] = YEAR(a.DateRange),
[Month] = MONTH(a.DateRange),
[DayOfMonth] = DAY(a.DateRange),
AbsWeekno = DATEDIFF(day,FirstWeekdayOffset,a.DateRange)/7,
[DayName] = DATENAME(weekday,a.DateRange)
) c
CROSS APPLY (
SELECT Holiday = CASE
WHEN [Month] = 1 AND [DayOfMonth] = 1 THEN 'New Year'
WHEN [Month] = 5 AND [DayOfMonth] >= 25 AND [DayName] = 'Monday' THEN 'Memorial Day'
WHEN [Month] = 7 AND [DayOfMonth] = 4 THEN 'Independence Day'
WHEN [Month] = 9 AND [DayOfMonth] <= 7 AND [DayName] = 'Monday' THEN 'Labor Day'
WHEN [Month] = 11 AND [DayOfMonth] BETWEEN 22 AND 28 AND [DayName] = 'Thursday' THEN 'Thanksgiving Day'
WHEN [Month] = 12 AND [DayOfMonth] = 25 THEN 'Christmas Day'
ELSE NULL END
) d
WHERE b.FirstWeekDay = @FirstWeekDay

)

GO



My son juggles whilst riding his unicycle. He earns cash from it, entertaining folks outside nightclubs. Nice little earner for a mech eng student.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Phil Parkin
Phil Parkin
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8347 Visits: 19489
DaveDB (1/24/2014)
This comment is very insulting.

"Perhaps the reason for my request for additional material is becoming clearer now.

The fact that accomplished professionals, who would usually be able to answer this question while simultaneously juggling on a unicycle, are having to guess your requirements is proof enough."



All I asked was.... "How can you filter Saturday and Sunday out of a query". You do not need a result set for this particular question and if you do.... well.... then it's on you. I am not looking for trouble, but in all honesty, you seem more interested in playing gatekeeper to SSC than actually answering questions. Did you really not understand my question? MSDN Forums didn't seem to have an issue.

* shrug


I just can't find an insult here. You obviously can. None was meant, I assure you.

Here is my quote, reworded: had you provided a detailed and specific question, it would have been answered by skilled, knowledgeable and experienced professionals with no fuss and no need for supplementary questions and guesswork on their part.

That's it.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search