November 18, 2013 at 9:57 am
Just bumped on this:
DECLARE @dob datetime
SET @dob='1992-01-09 00:00:00'
SELECT DATEDIFF(YEAR, '0:0', getdate()-@dob)
Works ok. Who can explain '0:0' ?
November 18, 2013 at 10:16 am
valeryk2000 (11/18/2013)
Just bumped on this:
DECLARE @dob datetime
SET @dob='1992-01-09 00:00:00'
SELECT DATEDIFF(YEAR, '0:0', getdate()-@dob)
Works ok. Who can explain '0:0' ?
Actually it doesn't really work ok. See the posts on the first page.
However, the '0:0' is simply a strange way of writing 1/1/1900 or the 0 date. Remember the second parameter is a datetime so it will perform an implicit conversion.
select cast('0:0' as datetime)
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
November 18, 2013 at 10:27 am
Thanks. So simple zero can work as well. So far I checked the code several times - and it was ok. Where is a potential error? May be I missed something on page 1 ...
November 18, 2013 at 1:01 pm
valeryk2000 (11/18/2013)
So far I checked the code several times - and it was ok. Where is a potential error?
Here's one of many places where it fails because of Leap Years...
DECLARE @dob datetime
,@now datetime
SELECT @dob = '2000-02-28 00:00:00'
,@now = '2001-02-27 00:00:00'
SELECT DATEDIFF(YEAR, '0:0', @now-@dob)
;
--Jeff Moden
Change is inevitable... Change for the better is not.
November 18, 2013 at 2:05 pm
Right ... it also does not work with infants (before they reach 1 year) ...
Thanks
November 18, 2013 at 5:23 pm
Old thread but this is my reference article on the subject:
Efficiently Querying and Calculating Using SQL Server Date/Time Columns --by Adam Machanic
Age is covered near the end.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
November 19, 2013 at 8:22 am
Very good article indeed. Learned a lot. I'm just taking on the "age" problem, it seems more complicated than I initially thought. I am working for a large health system including several hospitals, and age of patients may in years, months, weeks and even days (newborns). So I am going to write a universal function (or set of functions) that would return age in any of those units.
Thanks to everybody
Val
November 19, 2013 at 3:36 pm
valeryk2000 (11/19/2013)
Very good article indeed. Learned a lot. I'm just taking on the "age" problem, it seems more complicated than I initially thought. I am working for a large health system including several hospitals, and age of patients may in years, months, weeks and even days (newborns). So I am going to write a universal function (or set of functions) that would return age in any of those units.Thanks to everybody
Val
Cool. If you get the chance, post the function(s) when you're done. I'd love to see them. Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 19, 2013 at 5:25 pm
Jeff Moden (11/19/2013)
valeryk2000 (11/19/2013)
Very good article indeed. Learned a lot. I'm just taking on the "age" problem, it seems more complicated than I initially thought. I am working for a large health system including several hospitals, and age of patients may in years, months, weeks and even days (newborns). So I am going to write a universal function (or set of functions) that would return age in any of those units.Thanks to everybody
Val
Cool. If you get the chance, post the function(s) when you're done. I'd love to see them. Thanks.
So would I.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 9 posts - 16 through 23 (of 23 total)
You must be logged in to reply to this topic. Login to reply