Friday 13th with T-SQL – a “shortest” competition

Just for orientation, here is a quick T-SQL code to find out the next Fridays, that will be on 13th day of the month.

SET DATEFIRST 1

DECLARE @d DATE = GETDATE()

WHILE (@d < '2099/01/01')
    BEGIN
        IF DATEPART(dw,DATEFROMPARTS(YEAR(@d), MONTH(@d), 13)) = 5
            BEGIN
                SELECT @d AS Friday13
            END
        SET @d = DATEADD(m,1,@d)
    END

I set datefirst = 1 just in case you might have any other regional/language setting, noting that week starts with monday. In accordance with this, I can part the date and look for 5th day of the week (which it will be friday in this case).

Can you come up with shorter function in T-SQL that it would list dates of next couple of Friday the 13th? For example with OVER clause, CROSS APPLY, JOIN, CTE or  CURSOR? only imagination is the limit 🙂 Tally table can also be used.

Posted in Uncategorized
5 comments on “Friday 13th with T-SQL – a “shortest” competition
  1. Terry McCann says:

    How about :

    ;WITH lv0 AS ( SELECT 0 AS g UNION ALL SELECT 0 ) , –2
    lv1 AS ( SELECT 0 AS g FROM lv0 AS a CROSS JOIN lv0 AS b ) , — 2^ = 4
    lv2 AS ( SELECT 0 AS g FROM lv1 AS a CROSS JOIN lv1 AS b ) , — 4^ = 16
    lv3 AS ( SELECT 0 AS g FROM lv2 AS a CROSS JOIN lv2 AS b ) , — 16^ = 256
    lv4 AS ( SELECT 0 AS g FROM lv3 AS a CROSS JOIN lv3 AS b ) , — 256^ = 65,536
    Nums AS ( SELECT ROW_NUMBER() OVER ( ORDER BY ( SELECT NULL ) ) AS n FROM lv4 )
    SELECT DATEADD(DAY, n – 1, GETDATE()) AS ‘Friday13th’ FROM Nums
    WHERE DATEPART(DAY, DATEADD(DAY, n – 1, GETDATE())) = 13 AND DATEPART(dw, DATEADD(DAY, n – 1, GETDATE())) = 6

    Like

  2. Terry McCann says:

    This one is smaller

    ;WITH Nums AS (SELECT TOP 15000 ROW_NUMBER() OVER (ORDER BY a.object_name) n FROM sys.dm_os_performance_counters a CROSS JOIN sys.dm_os_performance_counters b)
    SELECT DATEADD(DAY, n – 1, GETDATE()) AS ‘Friday13th’ FROM Nums
    WHERE DATEPART(DAY, DATEADD(DAY, n – 1, GETDATE())) = 13 AND DATEPART(dw, DATEADD(DAY, n – 1, GETDATE())) = 5

    Like

  3. Terry McCann says:

    Ok 2 lines:

    ;WITH Nums AS (SELECT TOP 1000 DATEADD(Month,ROW_NUMBER() OVER (ORDER BY a.object_name)- 1, ‘20160101’ ) n FROM sys.dm_os_performance_counters a CROSS JOIN sys.dm_os_performance_counters b)
    SELECT n +12 AS ‘Friday13th’ FROM Nums WHERE DATEPART(dw, n) = 7

    Like

  4. Terry McCann says:

    SELECT n+12 FROM (SELECT DATEADD(M,ROW_NUMBER() OVER (ORDER BY id)-1,’20160101′)n FROM sys.syscolumns)N WHERE DATEPART(dw, n)=7

    Like

  5. tomaztsql says:

    So far the shortest was submitted on twitter by Mladen Prajdic: https://twitter.com/MladenPrajdic/status/731127126687125504

    with code:
    declare @d datetime=42501
    while @d<=73049
    begin
    set @d+=7
    if(day(@d)=13) select @d
    end

    Key here is setting date to 42501 (or any other Friday the 13th; in this it is today – 13.5.2016) and just increment by one week – it will always be Friday – until you find next friday on 13th day of the month.

    Congrats, Mladen.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories
Follow TomazTsql on WordPress.com
Programs I Use
Programs I Use
Programs I Use
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

Discover

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

tenbulls.co.uk

attaining enlightenment with sql server, .net, biztalk, windows and linux

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

Ms SQL Girl

Julie Koesmarno's Journey In Data, BI and SQL World

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

SQL Server, Azure and DLM in a nutshell :D

Paul te Braak

Business Intelligence Blog

Sql Server Insane Asylum (A Blog by Pat Wright)

Information about SQL Server from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

%d bloggers like this: