![]() |
| ||
| Problem in finding birthday from date of birth in SQL Server I have a database in SQL Server 2005.I've a table namely 'PersonalDetails' in it which contains a 'date of birth' field of type datetime.I've to generate weekly birthday reports . I wrote a sp like this: ALTER PROCEDURE [dbo].[selectweekfrompersonaldetails]but it does not work. Can u guide me in this |
| ||
| Re: Problem in finding birthday from date of birth in SQL Server Try this code select 1 as EmpID, 'John' as EmpName, '2007/01/01' as DateOfBirth into #tmpData union all select 2 as EmpID, 'Anne' as EmpName, '2007/01/05' as DateOfBirth union all select 3 as EmpID, 'Tony' as EmpName, '2007/01/08' as DateOfBirth union all select 4 as EmpID, 'Erin' as EmpName, '2007/02/28' as DateOfBirth union all select 5 as EmpID, 'Jane' as EmpName, '2007/03/01' as DateOfBirth declare @paramdate datetime select @paramdate = '2006/02/25' select EmpID, EmpName, month(dateofbirth) bmon,day(dateofbirth) bday, DateOfBirth, cast(cast(year(DateOfBirth) as varchar) + '/' + cast(month(@paramDate) as varchar)+ '/' + cast(day(@paramDate) as varchar) as smalldatetime) from #tmpData where DateOfBirth between cast(cast(year(DateOfBirth) as varchar) + '/' + cast(month(@paramDate) as varchar)+ '/' + cast(day(@paramDate) as varchar) as smalldatetime) and cast(cast(year(DateOfBirth) as varchar) + '/' + cast(month(@paramDate) as varchar)+ '/' + cast(day(@paramDate) as varchar) as smalldatetime) + 7 drop table #tmpData |
| ||
| Re: Problem in finding birthday from date of birth in SQL Server No need for all the separate hits/queries. SELECT EmpID, EmpName, DATEPART(m, DateOfBirth) As bmon, DATEPART(d, DateOfBirth) As bday FROM PersonalDetails WHERE DATEPART(d, DateOfBirth) BETWEEN DATEPART(d, @paramdate1) AND DATEPART(d, (DATEPART(d, 7, @paramdate1))) You need to be relatively careful of this method because it is very slow. If you have a lot of records, this task can take a very long time. The reason is that whenever you use formatting within the WHERE clause, it formats EVERY record, then does the search. Therefore if you have 2,000 records, it will format every single one, for every single where part (where in here you have 3 formats, so it will format it 6,000 times). To make this less and quicker, one at least add an Index on these fields (DateOfBirth), and then eliminate not possible candidates. So if you have candidates that you do not want (Like inactive employees), add a where clause in the BEGINNING, which will reduce the number of records formatted. Like below: SELECT EmpID, EmpName, DATEPART(m, DateOfBirth) As bmon, DATEPART(d, DateOfBirth) As bday FROM PersonalDetails WHERE EmpActive=1 AND DATEPART(d, DateOfBirth) BETWEEN DATEPART(d, @paramdate1) AND DATEPART(d, (DATEPART(d, 7, @paramdate1))) |
| All times are GMT -4. The time now is 7:01 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC