Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Age Expand / Collapse
Author
Message
Posted Tuesday, September 25, 2012 1:43 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:51 PM
Points: 32,906, Visits: 26,789
Michael Valentine Jones (9/25/2012)
You can use the function on the link below to find the age:
Age Function F_AGE_IN_YEARS
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462


Any chance of making it NOT a scalar function?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1364270
Posted Tuesday, September 25, 2012 2:42 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:18 PM
Points: 10,989, Visits: 10,532
Jeff Moden (9/25/2012)
Careful now... Because of the date conversions to the CHAR datatype, that will be relatively slow.

It is beautiful though - this has long been a favourite of mine (I think it was Rob Farley that first showed it to me).
As far as performance is concerned, well yes it will be slow. But then, all T-SQL solutions will be slow, right?
Just kidding (mostly) - it's a good point that conversion to string is particularly bad. And especially scalar T-SQL UDFs




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1364289
Posted Tuesday, September 25, 2012 3:18 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 2,944, Visits: 10,502
Jeff Moden (9/25/2012)
Michael Valentine Jones (9/25/2012)
You can use the function on the link below to find the age:
Age Function F_AGE_IN_YEARS
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462


Any chance of making it NOT a scalar function?


I originally wrote this for SQL 2000, so be my guest. The code could be greatly simplified too, probably enough to convert it to inline code.

I posted it mainly to give them an example of some code that I know I tested fairly completely.





Post #1364310
Posted Sunday, February 17, 2013 6:59 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:01 PM
Points: 4,540, Visits: 8,184
Jeff Moden (9/25/2012)
Michael Valentine Jones (9/25/2012)
You can use the function on the link below to find the age:
Age Function F_AGE_IN_YEARS
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462


Any chance of making it NOT a scalar function?


SELECT DATEDIFF(yy, 0, GETDATE() - DOB) Age_Way1, 
YEAR(GETDATE()-DOB) -1900 Age_Way2

Post #1421022
Posted Tuesday, February 19, 2013 3:44 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 4:41 PM
Points: 135, Visits: 719
My two bits...


declare @dob datetime
set @dob = '2/29/2000'

select convert(char(10),@dob,101) as DOB,
case
when ((MONTH(@dob) * 100) + DAY(@dob)) > ((MONTH(getdate()) * 100) + DAY(getdate()))
then DATEDIFF(year,@dob,getdate()) - 1
else DATEDIFF(year,@dob,getdate()) End as AgeInYears
Post #1421879
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse