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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ Register Now and get your question answered!
 Username: Password: Save Password Forgot your Password?
 All Forums  General SQL Server Forums  Script Library  Calculating age in years Reply to Topic  Printer Friendly
Author  Topic
Page: of 2

malaytech2008
Yak Posting Veteran

India
95 Posts

 Posted - 11/11/2008 :  00:37:43 You can try to find the Date of birth and modify this if any flaw u find..alter procedure Cal_DateofBirth_1 @dateofbirth datetimeasbegindeclare @EndDt datetime,@DOB datetimeset @EndDt=getdate()set @DOB=@dateofbirthif @EndDt >=@DOBselect case when day(@enddt) < day(@dob) then case when ((month(@enddt)-1) >= month(@dob)) then (year(@enddt)- year(@dob)) else (year(@enddt)- (year(@dob)+1)) end else case when (month(@enddt) >= month(@dob)) then (year(@enddt)- year(@dob)) else (year(@enddt)- (year(@dob))+1) end end as years, case when day(@enddt) < day(@dob) then case when ((month(@enddt)-1) >= month(@dob)) then ((month(@enddt)-1)- month(@dob)) else (month(@enddt)- month(@dob))+12 end else case when (month(@enddt) >= month(@dob)) then (month(@enddt)- month(@dob)) else (month(@enddt)- month(@dob))+12 end end as months, case when day(@enddt) >= day(@dob) then (day(@enddt)-day(@dob)) else case when month(@dob) in(1,3,5,7,8,10) then ((day(@enddt)+30)-day(@dob)) when month(@dob) in(4,6,9,11,12) then ((day(@enddt)+31)-day(@dob)) else case when (year(@dob)%4=0 or year(@dob)%100=0) then ((day(@enddt)+29)-day(@dob)) else ((day(@enddt)+28)-day(@dob)) end end end as dayselse print 'please provide valid date of birth'endmalay

Arnold Fribble
Yak-finder General

United Kingdom
1961 Posts

 Posted - 11/11/2008 :  04:33:32 quote:Originally posted by Michael Valentine JonesSQL Server seems to favor Feb 28:`select [One Year Birthday] = dateadd(year,1,'20040229')`Results:```One Year Birthday ------------------------------------------------------ 2005-02-28 00:00:00.000 (1 row(s) affected)```I think it's likely that's the case because someone decided that DATEADD(year, @n, @dt) should be equivalent to DATEADD(month, @n*12, @dt).

sendmejunks
Starting Member

1 Posts

 Posted - 05/17/2010 :  15:21:35 here's my versiondeclare @age int;declare @m int;set @age = 0;set @m = datediff(month,@dob,@Dateagainst) if datepart(day,@Dateagainst) < datepart(day,@dob) set @m = @m - 1; set @age = floor(@m/12) ; if @age < 0 set @age = null; return @age;

plcrewe
Starting Member

1 Posts

 Posted - 02/01/2011 :  11:16:43 To SELECT people's ages, I use the following SQL (NOTE: I've added the WHERE clause simply to show you how to view those people who are currently 39 years old. If you wish to view everybody then simply remove the WHERE clause from this statement).SELECT surname, birth_date, DATEDIFF(yy, birth_date, GETDATE()) - (CASE WHEN (DATEPART(m, birth_date) < DATEPART(m, GETDATE())) OR (DATEPART(m, birth_date) = DATEPART(m, GETDATE()) AND DATEPART(d, birth_date) <= DATEPART(d, GETDATE())) THEN 0 ELSE 1 END) AS ageFROM peopleWHERE DATEDIFF(yy, birth_date, GETDATE()) - (CASE WHEN (DATEPART(m, birth_date) < DATEPART(m, GETDATE())) OR (DATEPART(m, birth_date) = DATEPART(m, GETDATE()) AND DATEPART(d, birth_date) <= DATEPART(d, GETDATE())) THEN 0 ELSE 1 END) = 39ORDER BY 3

onecaring2001
Starting Member

1 Posts

 Posted - 10/12/2011 :  19:48:10 /*Just use this function and get the exact age in "YYYMMDD" format (YearsMonthsDays) average days in a year: 365.2425average days ion a month: 30.436875 USAGE: SELECT dbo.udf_CalcAge('1960-01-01', '2011-10-12')RESULT: "0510911" -- 51 YEARS, 9 MONTHS AND 11 DAYS */ CREATE FUNCTION [dbo].[udf_CalcAge] (@StartDate datetime = NULL, @EndDate datetime = NULL) RETURNS varchar(7) WITH EXECUTE AS CALLER ASBEGIN RETURN ( RIGHT('000' + CAST(FLOOR(DATEDIFF(dd, @StartDate, @EndDate) / 365.2425) AS VARCHAR(3)), 3)+ RIGHT('00' + CAST(FLOOR((DATEDIFF(dd, @StartDate, @EndDate) / 365.2425) % 1 * 12) AS VARCHAR(2)), 2) + RIGHT('00' + CAST(FLOOR(ROUND(((DATEDIFF(dd, @StartDate, @EndDate) / 365.2425) % 1 * 12) % 1 * 30.436875, 0)) AS VARCHAR(2)), 2) ) END;
Page: of 2  Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC
 This page was generated in 0.04 seconds.