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 2018 May SQL Puzzle: Get the list of Monday of a Month

SQL Puzzle: Get the list of Monday of a Month

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

Check the below input data and expected output to get the list of Monday of an April 2018

Expected Output: Get the list of Monday of April 2018

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
DayDate
----------
2018-04-02
 
DayDate
----------
2018-04-09
 
DayDate
----------
2018-04-16
 
DayDate
----------
2018-04-23
 
DayDate
----------
2018-04-30

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Declare @Month Bigint
Set @Month = 04
Declare @date date
Set @date='2018-04-01'
Declare @DayName VARCHAR(20)
Set @DayName = 'Monday'
 
while DATEPART(Month,@date)=@Month
Begin
DECLARE @Name VARCHAR(20)
 
SELECT @Name = CASE ( DATEPART(dw, @Date) + @@DATEFIRST ) % 7
WHEN 1 THEN 'Sunday'
WHEN 2 THEN 'Monday'
WHEN 3 THEN 'Tuesday'
WHEN 4 THEN 'Wednesday'
WHEN 5 THEN 'Thursday'
WHEN 6 THEN 'Friday'
WHEN 0 THEN 'Saturday'
END
If @Name=@DayName
Begin
Select @date as DayDate
End
Set @date=DATEADD(Day,1,@date)
End

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

May 4, 2018Anvesh Patel
SQL Puzzle: Find the upcoming Birthday for a Next MonthDBRND listed by feedspot in the Category of Top 30 SQL Server Blogs
Comments: 4
  1. VINUTNA
    May 6, 2018 at 2:47 pm

    declare @x date = ’04-01-2018′
    while (@x <= '04-30-2018')
    begin
    select @x=dateadd(day,1,@x)
    if datepart(dw,@x)=2
    begin
    select @x
    end
    end

    ReplyCancel
  2. Dpan
    April 15, 2019 at 9:27 am

    declare @dateOfApril date,@dayOfApril nvarchar(max)
    set @dateOfApril=’2018-04-01′;
    While (@dateOfApril<'2018-05-01')
    begin
    set @dayOfApril=DATENAME(dw,@dateOfApril)
    if(@dayOfApril='Monday')
    Begin
    print @dateOfApril;
    End
    set @dateOfApril=DateAdd(dd,1,@dateOfApril);
    end

    ReplyCancel
  3. HIC
    April 27, 2019 at 1:48 pm

    WITH days(mon) AS (
    SELECT next_day(to_date(‘31.03.2018’, ‘dd.mm.yyyy’), ‘monday’) as mon from dual
    UNION ALL
    SELECT mon + 7 FROM days
    WHERE mon + 7 <= to_date('01.05.2018', 'dd.mm.yyyy')
    )
    SELECT * FROM days;

    ReplyCancel
  4. Keith Fernandes
    October 27, 2019 at 2:17 pm

    declare @start date = ‘2018-04-01’
    declare @end date = ‘2018-04-30’
    ;with cte as
    (
    select @start as date
    union all
    select DATEADD(DAY,1,date) as date1 from cte
    where DATEADD(DAY,1,date) <= @end
    )
    select * from cte where DATENAME(DW,date) = 'Monday'

    ReplyCancel

Leave a Reply to VINUTNA Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel

Database Engineer

May 4, 2018 4 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....