|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Today @ 3:50 PM
Points: 171,
Visits: 1,400
|
|
I agree with others here. Using my birth date the only one that gave me the correct answer was the second one and my birthday is 7 months away.
I want my point! :)
Nicole Bowman
Nothing is forever.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Wednesday, August 08, 2012 1:18 PM
Points: 66,
Visits: 62
|
|
| The second answer is the most accurate of the three; however, it does not account for leap years. The third answer would be the correct answer if the 1 and 0 were switched around.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, July 31, 2009 4:31 AM
Points: 5,
Visits: 7
|
|
| the second one is correct. give me my point.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, February 21, 2013 4:06 PM
Points: 263,
Visits: 296
|
|
aarif.pd (4/10/2008) the second one is correct. give me my point.
No, it is not. :)
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
Goodness, this one's gone on, hasn't it? And now I'm in it cuz of the row-size QOTD that had a naive age calculation thrown in.
Anyway, before reading through all 19 pages, I coded this:
Declare @DOB smallDatetime Declare @Today smallDatetime set @DoB = '02/29/2004' set @Today = '2/29/08'
select @dob as Dob ,@today as Today ,year(@today)-year(@dob) - case when month(@today) < month(@dob) or (month(@today) = month(@dob) and day(@today) < day(@dob)) then 1 else 0 end as Age
It turns out that it's pretty similar to some other solutions agreed to be correct, except that I used the functions "year()", "month()", and "day()" rather than "Datepart()".
Can someone who knows tell me why Datepart() would be preferrable? I like the distinct functions for readability.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:13 PM
Points: 5,237,
Visits: 7,044
|
|
john.arnott (4/10/2008) Can someone who knows tell me why Datepart() would be preferrable? I like the distinct functions for readability.
Hi John,
YEAR(...) is synonym for DATEPART(year, ...); similar for DAY and MONTH. From a technical point of view, there's no preference.
The only thing left is readability. Some, like you, prefer the shorter versions because they are shorter and hence easier to understand. Others prefer DATEPART because you can also use that to expose dateparts that have no synonym (like, for instance, week, minute, quarter) and they consider it more consistent to use the same function in all cases.
All in all, just a matter of personal preference. Though I'd recommend you to follow shop standards if there are any.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 20, 2011 1:21 AM
Points: 27,
Visits: 139
|
|
| Love all the trick questions in the QOD stuff - I never even looked past the first one as even though it may not always give the correct birthdate in English/American terms, it could be adjusted to more readably do so by flooring the other values out of the year! Hand up, I got it wrong, but then IMO not a great question since the actual "correct" answer doesn't work either.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Sunday, March 18, 2012 1:38 AM
Points: 26,
Visits: 20
|
|
Declare @d smalldatetime set @d = '1957/7/5'
Select DATEDIFF(yy, @d, GETDATE()) - CASE WHEN DATEPART(m, @d) < DATEPART(m, GETDATE()) OR ( DATEPART(m, @d) = DATEPART(m, GETDATE()) AND DATEPART(d, @d) <= DATEPART(d, GETDATE()) ) THEN 0 ELSE 1 END
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 2:04 AM
Points: 1,342,
Visits: 1,946
|
|
Luc Philie (7/3/2008) Declare @d smalldatetime set @d = '1957/7/5'
Select DATEDIFF(yy, @d, GETDATE()) - CASE WHEN DATEPART(m, @d) < DATEPART(m, GETDATE()) OR ( DATEPART(m, @d) = DATEPART(m, GETDATE()) AND DATEPART(d, @d) <= DATEPART(d, GETDATE()) ) THEN 0 ELSE 1 END And your point is...?
Derek
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, June 16, 2010 1:09 AM
Points: 218,
Visits: 15
|
|
declare @DateOfBirth datetime declare @result as varchar set @DateOfBirth='1983-07-11'
select @result = (DATEDIFF(yy, @DateOfBirth, GETDATE()) - case when Convert(Datetime,Convert(varchar,DATEPART(yy, GETDATE())) + '-' + Convert(varchar,DATEPART(m, @DateOfBirth))+ '-'+ Convert(varchar,DATEPART(d, @DateOfBirth))) <=GetDate() THEN 0 ELSE 1 END ) select @Result
Here is the correct query to retrieve exact age.
MayurArt
|
|
|
|