Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 October SQL Puzzle: SQL Advance Query – Find the date of all third Sunday for Year 2017

SQL Puzzle: SQL Advance Query – Find the date of all third Sunday for Year 2017

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to find the date of Third Sunday of all the months.

Input Year:

1
2017

Expected Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sundays
----------
2017-01-15
2017-02-19
2017-03-19
2017-04-16
2017-05-21
2017-06-18
2017-07-16
2017-08-20
2017-09-17
2017-10-15
2017-11-19
2017-12-17

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
;WITH CTE AS
(
SELECT
DATEFROMPARTS(2017,1,1) FirstDay
,DATENAME(DW, DATEFROMPARTS(2017,1,1)) Day_Name
UNION ALL
SELECT
DATEADD(d,1,FirstDay) FirstDay
,DATENAME(DW, DATEADD(d,1,FirstDay)) Day_Name
FROM CTE
WHERE FirstDay < DATEFROMPARTS(2017,12,31)
)
SELECT FirstDay as Sundays
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY Months ORDER BY Months) rnk
FROM
(
SELECT *, MONTH(FirstDay) Months FROM CTE WHERE Day_Name = 'Sunday'
) t
)t WHERE rnk = 3
OPTION (MAXRECURSION 0)

Please try the different solution for this puzzle and share it via comment...

Oct 23, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Check number is an Integer or not an IntegerSQL Puzzle: SQL Advance Query - Find the average shared salary by each department
Comments: 2
  1. Dinesh.IS
    October 24, 2017 at 5:34 am

    Declare @StartDate DateTime
    Declare @EndDate DateTime;

    Set @StartDate=’2017/01/01′
    Set @EndDate=’2017/12/31′
    ;
    WITH CTE
    As
    (Select @StartDate As StartDate,DateName(DW,@StartDate) As Day_Name
    Union All
    Select DateAdd(Day,1,StartDate),DateName(DW,DateAdd(Day,1,StartDate)) As Day_Name
    From CTE Where StartDate<@EndDate)
    Select StartDate From (Select Row_Number()Over(Partition By Month(StartDate) Order By StartDate) As RSLNo,* From CTE Where Day_Name='SunDay') A
    Where RSLNo=3
    OPTION(MAXRECURSION 0)

    ReplyCancel
  2. Nitin patel
    August 25, 2018 at 11:54 am

    declare @startdate datetime =’01-01-2017′
    declare @enddate datetime =’12-31-2017′
    ;with cte as
    (
    select CAst(dateadd(day,case datename(WEEKDAY,@startdate) when ‘Sunday’ then 14 when ‘Monday’ then 20 when ‘Tuesday’ then 19
    when ‘wednesday’ then 18 when ‘thursday’ then 17 when ‘Friday’ then 16 else 15 end,@startdate) as datetime) dt1
    union all
    select cast(dateadd(day,case datename(WEEKDAY,( dateadd (day,-(DATEPART(dd,(eomonth(DATEADD (MONTH,1,dt1)))))+1,eomonth(DATEADD(MONTH,1,dt1))))) when ‘Sunday’ then 14 when ‘Monday’ then 20 when ‘Tuesday’ then 19
    when ‘wednesday’ then 18 when ‘thursday’ then 17 when ‘Friday’ then 16 else 15 end, ( dateadd (day,-(DATEPART(dd,(eomonth(DATEADD (MONTH,1,dt1)))))+1,eomonth(DATEADD(MONTH,1,dt1))))) as datetime)
    from cte where dt1< @enddate
    )

    select * from cte

    ReplyCancel

Leave a Reply to Dinesh.IS Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 23, 2017 2 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....