0

This is the question. I am getting the result as number of days from the date of birth till now. but I need whether it is upcoming 5 days from the date now.

select *, datediff(DAY , DateOfBirth,GETDATE()) as diff
    From Table_A
    order by DateOfBirth asc
Dmitrij Kultasev
  • 5,447
  • 5
  • 44
  • 88
vishal
  • 1
  • 3

3 Answers3

0
SELECT *
FROM Table_A WHERE DateOfBirth BETWEEN GETDATE()
   AND DATEADD(day,5, Convert(Date,GETDATE()))

it will give you all the birthdays in the next 5 days.

wonea
  • 4,783
  • 17
  • 86
  • 139
0

If you are looking for the birthdays, you can do something like:

DECLARE @dates AS TABLE ( DateOfBirth DATE );
DECLARE @number_of_days INT = 5;

INSERT  INTO @dates
        ( DateOfBirth )
VALUES  ( '20090101' ),
        ( '20090809' ),
        ( '20090816' ),
        ( '20180807' );

SELECT  * ,
        DATEDIFF(DAY, SYSDATETIME(), bd.this_year_birthday)
FROM    @dates d
        CROSS APPLY ( SELECT    DATEFROMPARTS(YEAR(SYSDATETIME()),
                                              MONTH(d.DateOfBirth),
                                              DAY(d.DateOfBirth)) AS this_year_birthday
                    ) bd
WHERE   DATEDIFF(DAY, SYSDATETIME(), bd.this_year_birthday) BETWEEN 0
                                                            AND
                                                              @number_of_days;
Dmitrij Kultasev
  • 5,447
  • 5
  • 44
  • 88
-1

As per my understanding, you need to get upcoming birthdays in next 5 days. In MySQL you can do with the query below,

SELECT *, DATE_FORMAT(DateOfBirth,'%m-%d') AS md 
FROM Table_A 
WHERE DATE_FORMAT(DateOfBirth,'%m-%d') BETWEEN '08-07' AND '08-12'
ORDER BY md

It will return all birthdays between 07 and 12 Aug. Get the current and +5 days at the scripting side.

If you want pure query based solution without any calculation for dates at scripting side, refer following query,

SELECT *, DATE_FORMAT(DateOfBirth,'%m-%d') AS md 
FROM Table_A 
WHERE DATE_FORMAT(DateOfBirth,'%m-%d') BETWEEN DATE_FORMAT(CURDATE(), '%m-%d')
AND DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 5 DAY), '%m-%d')
ORDER BY md
Samir Selia
  • 7,007
  • 2
  • 11
  • 30