Get Next 7 Days Birthday Data in SQL

Bipin kumar 1 Reputation point
2022-08-18T19:26:16.39+00:00

-2

I have Date of birth column in SQL where Year part is defaulted to 2296 for all the rows.

Now the requirement is to get Next 7 days records from current date using SQL for reporting.

How to write SQL query to get the desired result?

Key part to notice here is year set to default value 2296 for all rows. And this was done for compliance reason.

I had 3 requirements- 1. Today's Birthday - Select Id from tableA where Day(dob)=Day(GetDate()) And Month(dob)= Month(GetDate())

  1. This Month's Birthday - Select Id from tableB where Day(dob)>=Day(GetDate()) And Month(dob)= Month(GetDate())

I am really stuck with the 3rd requirement which I am posting in my original post.

Developer technologies | Transact-SQL
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-08-18T19:37:36.883+00:00

    Try something like this:

    declare @start date = datefromparts(2296, month(getdate()), day(getdate()))  
    declare @end date = dateadd(day, 6, @start)  
      
    select * from TableA where dob between @start and @end  
    

    It assumes that there is no time part.


  2. LiHongMSFT-4306 31,566 Reputation points
    2022-08-19T01:50:14.527+00:00

    Hi @Bipin kumar

    Year part is defaulted to 2296 for all the rows
    the requirement is to get Next 7 days records from current date

    You could replace the Year '2296' with current year '2022' first and then do a filter, like this:

    SELECT * FROM TableC   
    WHERE REPLACE(dob,'2296',YEAR(GETDATE())) BETWEEN CONVERT(DATE, GETDATE()) AND CONVERT(DATE,DATEADD(DAY,6,GETDATE()))  
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Viorel 122.6K Reputation points
    2022-08-19T11:18:14.047+00:00

    Check a revised query:

    declare @start1 date = datefromparts(2296, month(getdate()), day(getdate()))  
    declare @end1 date = dateadd(day, 6, @start1)  
          
    declare @start2 date = datefromparts(2295, month(getdate()), day(getdate()))  
    declare @end2 date = dateadd(day, 6, @start2)  
          
    select * from TableA   
    where dob between @start1 and @end1  
    or dob between @start2 and @end2  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.