Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need SQL Query for upcoming birthdays

Author  Topic 

pankaj.pareek@sparrowi.co
Starting Member

26 Posts

Posted - 2008-09-24 : 08:28:39
Dear All,

I need a SQL QUERY for fetching all upcoming birthdays within next 20 days.

The Structure of my table is :

CREATE TABLE [dbo].[tbl_EMPXYZ]
(
[E_code] [nvarchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[FirstName] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[MiddleName] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[LastName] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DOB] [smalldatetime] NULL,[BirthPlace] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[BirthCountry] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Nationality] [nvarchar](200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]



Thanks
Rishi Sharma

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-24 : 08:39:42
[code]SELECT * FROM [dbo].[tbl_EMPXYZ]
WHERE MONTH(DOB) BETWEEN MONTH(GETDATE()) AND MONTH(DATEADD(dd,20,GETDATE())) AND
DAY(DOB) BETWEEN DAY(GETDATE()) AND
DAY(DATEADD(dd,20,GETDATE())) [/code]
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-09-24 : 08:44:44
Declare @fromdate datetime, @todate datetime

select @fromdate = Getdate(), @todate = Getdate()+20

SELECT @FROMDATE = CAST( CONVERT( VARCHAR(12), @FROMDATE, 101 ) AS DATETIME )
SELECT @TODATE = CAST( CONVERT( VARCHAR(12), @TODATE, 101 ) AS DATETIME )

select E_code,FirstName,MiddleName,LastName,DOB,BirthCountry,Nationality
from dbo.tbl_EMPXYZ
WHERE ( (CAST( CONVERT( VARCHAR(12), DOB, 101 ) AS DATETIME ) >= @fromdate )
AND ( CAST( CONVERT( VARCHAR(12), DOB, 101 ) AS DATETIME ) <= @Todate ) )
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-24 : 08:46:07
The easiest way is to manipulate the birthday to that it is a date in 2000 (a leap year), and then get your start/end dates in 2000 as well, and then compare.

so, to set the DOB so it is a date in 2000, you can use DateAdd():

select dateadd(year, 2000 - year(dob), DOB) as DOB_In_2000
from tbl_EMPXYZ


Now, just figure out your start/end dates in the year 2000:

declare @start datetime
declare @end date

set @start = dateadd(year, 2000-year(getdate()), getdate())
set @end = @start + 20

and from there you can write your sql like this:

select * from yourtable
where dateadd(year, 2000 - year(dob), DOB) between @start and @end

That's one basic way of doing it. tweak as needed.

We must use a leap year because if any dates in our calculations fall on Feb 29th, it may cause issues.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-24 : 08:48:29
quote:
Originally posted by visakh16

SELECT * FROM [dbo].[tbl_EMPXYZ] 
WHERE MONTH(DOB) BETWEEN MONTH(GETDATE()) AND MONTH(DATEADD(dd,20,GETDATE())) AND
DAY(DOB) BETWEEN DAY(GETDATE()) AND
DAY(DATEADD(dd,20,GETDATE()))




Doesn't work if the start date and the end date have different months.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-24 : 08:49:35
quote:
Originally posted by raky

Declare @fromdate datetime, @todate datetime

select @fromdate = Getdate(), @todate = Getdate()+20
select @fromdate,@todate

select E_code,FirstName,MiddleName,LastName,DOB,BirthCountry,Nationality
from dbo.tbl_EMPXYZ
WHERE ( (@fromdate IS NULL OR CAST( CONVERT( VARCHAR(12), DOB, 101 ) AS DATETIME ) >= @fromdate )
AND ( @Todate IS NULL OR CAST( CONVERT( VARCHAR(12), DOB, 101 ) AS DATETIME ) <= @Todate ) )



Why would @FromDate or @ToDate be null?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-09-24 : 08:57:22
I believe I saw following query from Peso in one of the post.

DECLARE @sample TABLE
(
dob datetime
)

INSERT INTO @sample (dob)
SELECT '19000831' UNION ALL
SELECT '19000901' UNION ALL
SELECT '19000902' UNION ALL
SELECT '19000903' UNION ALL
SELECT '19000925'

SELECT *
FROM
(
SELECT *,
bd_this_year = DATEADD(YEAR, DATEPART(YEAR, GETDATE()) - DATEPART(YEAR, dob), dob)
FROM @sample s
) d
WHERE d.bd_this_year > DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND d.bd_this_year <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 20)

Hope this helps.
Go to Top of Page

pankaj.pareek@sparrowi.co
Starting Member

26 Posts

Posted - 2008-09-26 : 00:21:46
Hi Jeff,

Not Working Properly, working only next three month.......

changed the system date '26-09-2008' to '26-12-2008' & check not working.

Regards
Rishi Sharma





quote:
Originally posted by jsmith8858

The easiest way is to manipulate the birthday to that it is a date in 2000 (a leap year), and then get your start/end dates in 2000 as well, and then compare.

so, to set the DOB so it is a date in 2000, you can use DateAdd():

select dateadd(year, 2000 - year(dob), DOB) as DOB_In_2000
from tbl_EMPXYZ


Now, just figure out your start/end dates in the year 2000:

declare @start datetime
declare @end date

set @start = dateadd(year, 2000-year(getdate()), getdate())
set @end = @start + 20

and from there you can write your sql like this:

select * from yourtable
where dateadd(year, 2000 - year(dob), DOB) between @start and @end

That's one basic way of doing it. tweak as needed.

We must use a leap year because if any dates in our calculations fall on Feb 29th, it may cause issues.



- Jeff
http://weblogs.sqlteam.com/JeffS


Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-09-26 : 05:28:20

Have u tried my solution ??
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-26 : 05:34:02
See this prewritten function.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80709



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-26 : 05:37:08
[code]SELECT e_code,
years
from (
SELECT e_Code,
dbo.fnHasBirthday(DoB, GETDATE(), GETDATE() + 20) AS years
FROM tbl_EMPXYZ
) AS w
where years is not null[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-26 : 05:41:37
quote:
Originally posted by sunil

I believe I saw following query from Peso in one of the post.

DECLARE @sample TABLE
(
dob datetime
)

INSERT INTO @sample (dob)
SELECT '19000831' UNION ALL
SELECT '19000901' UNION ALL
SELECT '19000902' UNION ALL
SELECT '19000903' UNION ALL
SELECT '19000925'

SELECT *
FROM
(
SELECT *,
bd_this_year = DATEADD(YEAR, DATEPART(YEAR, GETDATE()) - DATEPART(YEAR, dob), dob)
FROM @sample s
) d
WHERE d.bd_this_year > DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND d.bd_this_year <= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 20)

Hope this helps.


Unbelivable what people find these days...

I can't take credit for this.

khtan wrote it here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=109812
Good approach but must be rewritten for use of leap years as jeff wrote above.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-09-26 : 05:51:11
quote:
Originally posted by Peso

[quote]Originally posted by sunil
Unbelivable what people find these days...

Too bad I can't take credit for this.

khtan wrote it here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=109812


E 12°55'05.63"
N 56°04'39.26"




I keep copy of solutions I feel can be used in near future and to understand how it was solved. It has many given by you,so mentioned same. I should keep name as well with solution.
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2008-09-26 : 10:49:22
This is one of my all-time favorite coding problems, because it sounds like it should be SO SIMPLE TO DO, when in reality it is deceptively complex.
This is what I came up with a while back, and I think I tested it to handle leap years:
create function [dbo].[LastAnniversaryAsOf](@Anniversary datetime, @AsOfDate datetime)
returns datetime as
begin
/*
------------------------------------------------------------------------------------------------------------------------
FUNCTION: LastAnniversaryAsOf
DESCRIPTION: Returns the last anniversary as of a specified date.
------------------------------------------------------------------------------------------------------------------------
REVISION HISTORY:
EDITOR DATE REVISIONS
blindman 10/3/2007 Function created
------------------------------------------------------------------------------------------------------------------------
*/

--Test parameters
--declare @Anniversary datetime
--declare @AsOfDate datetime
--set @Anniversary = '6/10/1965'
--set @AsOfDate = getdate()

return
(select max(Anniversary) as LastAnniversary
from
(select dateadd(year, datediff(year, @Anniversary, @AsOfDate)-1, @Anniversary) as Anniversary
UNION
select dateadd(year, datediff(year, @Anniversary, @AsOfDate), @Anniversary) as Anniversary) Candidates
where Anniversary < @AsOfDate)

end
go

create function [dbo].[NextAnniversaryAsOf](@Anniversary datetime, @AsOfDate datetime)
returns datetime as
begin
/*
------------------------------------------------------------------------------------------------------------------------
FUNCTION: NextAnniversaryAsOf
DESCRIPTION: Returns the Next anniversary as of a specified date.
------------------------------------------------------------------------------------------------------------------------
REVISION HISTORY:
EDITOR DATE REVISIONS
blindman 10/3/2007 Function created
------------------------------------------------------------------------------------------------------------------------
*/

--Test parameters
--declare @Anniversary datetime
--declare @AsOfDate datetime
--set @Anniversary = '6/10/1965'
--set @AsOfDate = getdate()

return
(select min(Anniversary) as NextAnniversary
from
(select dateadd(year, datediff(year, @Anniversary, @AsOfDate)+1, @Anniversary) as Anniversary
UNION
select dateadd(year, datediff(year, @Anniversary, @AsOfDate), @Anniversary) as Anniversary) Candidates
where Anniversary > @AsOfDate)

end


Boycotted Beijing Olympics 2008
Go to Top of Page

mcrowley
Aged Yak Warrior

771 Posts

Posted - 2008-09-26 : 14:58:44
This seems to give a rough idea of birthdays in the next 20 days, but it does drift with leap years. How accurate you need this to be is a question...

declare @days int
set @days = 20
select *
from tbl_EMPXYZ
where 365 - datediff(dd, dob, getdate()) % 365 < @days
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-26 : 15:05:15
quote:
Originally posted by pankaj.pareek@sparrowi.co

Hi Jeff,

Not Working Properly, working only next three month.......

changed the system date '26-09-2008' to '26-12-2008' & check not working.

Regards
Rishi Sharma




Why isn't it working properly?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

pankaj.pareek@sparrowi.co
Starting Member

26 Posts

Posted - 2008-09-29 : 01:40:41
Your Solution works for me. I am not so tight with leap years.

thankyou very much.

Regards
Rishi Sharma



quote:
Originally posted by mcrowley

This seems to give a rough idea of birthdays in the next 20 days, but it does drift with leap years. How accurate you need this to be is a question...

declare @days int
set @days = 20
select *
from tbl_EMPXYZ
where 365 - datediff(dd, dob, getdate()) % 365 < @days


Go to Top of Page
   

- Advertisement -