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.
| 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] |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-09-24 : 08:44:44
|
| Declare @fromdate datetime, @todate datetimeselect @fromdate = Getdate(), @todate = Getdate()+20SELECT @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_EMPXYZWHERE ( (CAST( CONVERT( VARCHAR(12), DOB, 101 ) AS DATETIME ) >= @fromdate ) AND ( CAST( CONVERT( VARCHAR(12), DOB, 101 ) AS DATETIME ) <= @Todate ) ) |
 |
|
|
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_2000from tbl_EMPXYZNow, just figure out your start/end dates in the year 2000:declare @start datetimedeclare @end dateset @start = dateadd(year, 2000-year(getdate()), getdate())set @end = @start + 20and from there you can write your sql like this:select * from yourtablewhere dateadd(year, 2000 - year(dob), DOB) between @start and @endThat'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.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
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.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2008-09-24 : 08:49:35
|
quote: Originally posted by raky Declare @fromdate datetime, @todate datetimeselect @fromdate = Getdate(), @todate = Getdate()+20select @fromdate,@todateselect E_code,FirstName,MiddleName,LastName,DOB,BirthCountry,Nationality from dbo.tbl_EMPXYZWHERE ( (@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?- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
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 ALLSELECT '19000901' UNION ALLSELECT '19000902' UNION ALLSELECT '19000903' UNION ALLSELECT '19000925'SELECT *FROM( SELECT *, bd_this_year = DATEADD(YEAR, DATEPART(YEAR, GETDATE()) - DATEPART(YEAR, dob), dob) FROM @sample s) dWHERE 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. |
 |
|
|
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.RegardsRishi Sharmaquote: 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_2000from tbl_EMPXYZNow, just figure out your start/end dates in the year 2000:declare @start datetimedeclare @end dateset @start = dateadd(year, 2000-year(getdate()), getdate())set @end = @start + 20and from there you can write your sql like this:select * from yourtablewhere dateadd(year, 2000 - year(dob), DOB) between @start and @endThat'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.- Jeffhttp://weblogs.sqlteam.com/JeffS
|
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-09-26 : 05:28:20
|
| Have u tried my solution ?? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-26 : 05:37:08
|
[code]SELECT e_code, yearsfrom ( SELECT e_Code, dbo.fnHasBirthday(DoB, GETDATE(), GETDATE() + 20) AS years FROM tbl_EMPXYZ ) AS wwhere years is not null[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
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 ALLSELECT '19000901' UNION ALLSELECT '19000902' UNION ALLSELECT '19000903' UNION ALLSELECT '19000925'SELECT *FROM( SELECT *, bd_this_year = DATEADD(YEAR, DATEPART(YEAR, GETDATE()) - DATEPART(YEAR, dob), dob) FROM @sample s) dWHERE 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=109812Good approach but must be rewritten for use of leap years as jeff wrote above. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-09-26 : 05:51:11
|
quote: Originally posted by Peso [quote]Originally posted by sunilUnbelivable 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. |
 |
|
|
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 asbegin/*------------------------------------------------------------------------------------------------------------------------FUNCTION: LastAnniversaryAsOfDESCRIPTION: Returns the last anniversary as of a specified date.------------------------------------------------------------------------------------------------------------------------REVISION HISTORY:EDITOR DATE REVISIONSblindman 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 LastAnniversaryfrom(select dateadd(year, datediff(year, @Anniversary, @AsOfDate)-1, @Anniversary) as AnniversaryUNIONselect dateadd(year, datediff(year, @Anniversary, @AsOfDate), @Anniversary) as Anniversary) Candidateswhere Anniversary < @AsOfDate)endgocreate function [dbo].[NextAnniversaryAsOf](@Anniversary datetime, @AsOfDate datetime)returns datetime asbegin/*------------------------------------------------------------------------------------------------------------------------FUNCTION: NextAnniversaryAsOfDESCRIPTION: Returns the Next anniversary as of a specified date.------------------------------------------------------------------------------------------------------------------------REVISION HISTORY:EDITOR DATE REVISIONSblindman 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 NextAnniversaryfrom(select dateadd(year, datediff(year, @Anniversary, @AsOfDate)+1, @Anniversary) as AnniversaryUNIONselect dateadd(year, datediff(year, @Anniversary, @AsOfDate), @Anniversary) as Anniversary) Candidateswhere Anniversary > @AsOfDate)end Boycotted Beijing Olympics 2008 |
 |
|
|
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 intset @days = 20select *from tbl_EMPXYZwhere 365 - datediff(dd, dob, getdate()) % 365 < @days |
 |
|
|
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.RegardsRishi Sharma
Why isn't it working properly?- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
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.RegardsRishi Sharmaquote: 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 intset @days = 20select *from tbl_EMPXYZwhere 365 - datediff(dd, dob, getdate()) % 365 < @days
|
 |
|
|
|
|
|
|
|