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: Find the upcoming Birthday for a Next Month

SQL Puzzle: Find the upcoming Birthday for a Next Month

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

Check the below input data and expected output for finding the upcoming birthdays of students.

Input Data:

1
2
3
4
5
6
7
8
EmpID EmpName DOB
----------- ---------- ----------
1 Anvesh 1988-01-26
2 Jenny 1991-04-09
3 Alpesh 1993-06-10
4 Roy 1991-02-14
5 Manish 1985-03-05
6 Nupur 1988-08-08

Expected Output for May 2018:

1
2
3
EmpID EmpName DOB
----------- ---------- ----------
5 Manish 1985-05-05

Create a table:

1
2
3
4
5
CREATE TABLE tbl_Employee (EmpID INT, EmpName VARCHAR(10), DOB DATE)
 
INSERT INTO tbl_Employee VALUES
(1,'Anvesh','19880126'),(2,'Jenny','19910409'),(3,'Alpesh','19930610')
,(4,'Roy','19910214'),(5,'Manish','19850505'),(6,'Nupur','19880808')

Solution:

1
2
3
SELECT * FROM tbl_Employee
WHERE DATEADD( Year, DATEPART( Year, GETDATE()) - DATEPART( Year, DOB), DOB)
BETWEEN CONVERT( DATE, GETDATE()) AND CONVERT( DATE, GETDATE() + 30)

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

May 3, 2018Anvesh Patel
SQL Server: Using xp_cmdshell, Read the data of Text FileSQL Puzzle: Get the list of Monday of a Month
Comments: 6
  1. Dinesh
    May 4, 2018 at 1:08 pm

    –Methos-1
    Select * From TBL_Employee
    Where Convert(Date,Replace(Convert(VarChar,DOB),Year(DOB),Year(GetDate())))
    Between Convert(Date,GetDate()) And Convert(Date,GetDate()+30)

    –Method-2
    Select * From TBL_Employee
    Where Convert(Date,Convert(VarChar(4),Year(GetDate()))+Right(DOB,6)) Between Convert(Date,GetDate()) And Convert(Date,GetDate()+30)

    –Method-3
    Select * From TBL_Employee
    Where Convert(Date,Convert(VarChar(4),Year(GetDate()))+’/’+Cast(Month(DOB) As VarChar(2))+’/’+Cast(Day(DOB) As VarChar(2))) Between Convert(Date,GetDate()) And Convert(Date,GetDate()+30)

    ReplyCancel
  2. aravind
    May 4, 2018 at 5:42 pm

    select *,current_date from employees where extract(‘month’ from dob)>=extract(‘month’ from current_date::date) order by dob desc limit 1;

    ReplyCancel
  3. karthik
    November 1, 2018 at 2:55 pm

    select EmpName ,DOB from tbl_Employee where datename(MONTH , DOB) = datename(MONTH , DATEADD( MONTH,1, getdate()))

    ReplyCancel
  4. Dpan
    April 15, 2019 at 9:41 am

    ;with BDay as (
    select Empid,EmpName,DOb,DateDiff(dd,GETDATE(),stuff(Dob,1,4,2019)) as UpcomingDate from #tbl_employee
    )
    select Top 1 * from BDay where UpcomingDate>0 Order by UpcomingDate

    ReplyCancel
  5. HIC
    April 27, 2019 at 6:03 pm

    select e1.*
    from
    tbl_Employee e1
    where e1.dob = (
    select min(e.dob)
    from
    tbl_Employee e
    where
    add_months(e.dob, (EXTRACT(year FROM sysdate) – EXTRACT(year FROM e.dob)) * 12) >= sysdate
    );

    ReplyCancel
  6. Hareesh
    January 6, 2020 at 4:57 pm

    select * from tbl_Employee
    where MONTH(DOB) = MONTH(Getdate())+1

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

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