Sql Birthdate Query

In this post you will learn how to find birth date of customer or student. We will use getdate(), month and day functions in the query. Month function returns back the month of the date.(1-12 values). Day function returns back the day of the date. Getdate() function returns the moment the query has run.

TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK

Example 1: List students whose birth date is today

Result for 03 Nowember 2018:

Example 2: List students whose birth date is tomorrow

Result for 03 Nowember 2018:

Example 3: List students whose birth date is in next week

Result for 03 Nowember 2018:

NOT:: The query above not exactly true. Not works on leap years and the last week of year. The query below is long but exactly true;

Result for 03 Nowember 2018

YOU MAY WANT TO SEE OUR ALL EXAMPLES PAGE, THEN CLICK HERE

 

1 Comment

  • Thank you for the examples. Finally, I wrote this version:
    declare @StarDayOfYear int = DATEPART(dy, dateadd(dd,1,getdate()));
    declare @EndDayOfYear int = DATEPART(dy, dateadd(dd,7,getdate()));

    Select * from students where
    DATEPART(dy, birthdate) between @StarDayOfYear and @EndDayOfYear

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: