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


When will the next 13th fall on a Friday?


When will the next 13th fall on a Friday?

Author
Message
nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2823
An this is slightly faster again, by avoiding the string comparison ( = 'Friday'):

    SELECT 
YEAR(DATEADD(mm,N-1,'17530113')),
COUNT(*)
FROM
dbo.Tally
WHERE
N < DATEDIFF(mm,'17530113','99990101')
AND
(DATEPART(dw,DATEADD(mm,N-1,'17530113')) + @@DATEFIRST) % 7 = 6
GROUP BY
YEAR(DATEADD(mm,N-1,'17530113'))
HAVING
count(*) = 3
ORDER BY
YEAR(DATEADD(mm,N-1,'17530113'))



--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Bill Coale-478581
Bill Coale-478581
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 117
Nigel,
Yes, your tally approach is much faster. 100 ms vs 1400 ms on my machine.
Thanks,
Bill
RakeshRSingh
RakeshRSingh
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2827 Visits: 208
If the current date is equal to 13 then your query will give that as Friday.

IF (DATEPART(dd, @currDate) <> 13)


what about adding one more condition with above one
OR (DATEPART(dw, @currDate) <> 1)

then I think your query is perfect.

:-)
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