SQL: Find people’s birthday which is today or a date range


SELECT DATEADD(day, DATEDIFF(day,0,GETDATE())-1,0) as StartOfYesterday

SELECT DATEADD(day, DATEDIFF(day,0,GETDATE()),0) as StartOfToday

SELECT DATEADD(day, DATEDIFF(day,0,GETDATE())+1,0) as StartOfNextday

SELECT * FROM students
WHERE
CASE
  WHEN Dateadd(YEAR, Datediff(YEAR, dob, Getdate()), dob) > Dateadd(DAY, Datediff(DAY, 0, Getdate()), 0) THEN Dateadd(YEAR, Datediff(YEAR, dob, Getdate()), dob)
  ELSE Dateadd(YEAR, Datediff(YEAR, dob, Getdate()) + 1, dob)
END BETWEEN — StartOfToday
Dateadd(DAY, Datediff(DAY, 0, Getdate()) , 0) AND
— StartOfNextday
Dateadd(DAY, Datediff(DAY, 0, Getdate()) + 1, 0)

–Refer Michael Valentine Jones:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=70975

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s