Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Incluse Saturday and Sunday in calculation Expand / Collapse
Author
Message
Posted Wednesday, January 15, 2014 8:05 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:05 AM
Points: 2,036, Visits: 1,378
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
Post #1531125
Posted Friday, January 24, 2014 9:07 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, February 24, 2014 11:06 AM
Points: 109, 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
Post #1534536
Posted Friday, January 24, 2014 9:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:51 AM
Points: 7,127, Visits: 13,503
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
Post #1534555
Posted Friday, January 24, 2014 10:44 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 3:14 AM
Points: 4,977, Visits: 11,669
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.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1534588
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse