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 ««12345»»»

Calculating Age Expand / Collapse
Author
Message
Posted Monday, June 30, 2008 8:38 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, August 6, 2013 1:55 PM
Points: 3, Visits: 16
I got this error from that code:

Msg 102, Level 15, State 1, Line 7
Incorrect syntax near '–'.

Post #525993
Posted Monday, June 30, 2008 9:09 AM


SSC-Dedicated

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

Group: Administrators
Last Login: Today @ 10:27 AM
Points: 31,040, Visits: 15,470
Try deleting the hyphen and re-entering it. Sometimes converting from SSMS to Word to our editor and HTML gets strange replacements for characters. I think this was one of those cases.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #526024
Posted Monday, June 30, 2008 11:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 5, Visits: 52
The posted codes work fine for full years, but I have been trying to find good SQL code that will calculate down to days if needed (for babies). I a baby is under a couple weeks old, I need to report how many days old they are. If the baby is older than that, but under a few months old, I need to report how many weeks old they are. If the child is older than a few months, but less than a year, I have to report how many months old they are. I haven't been able yet to come up with code that will do that and display something like (as applicable):

3d or
2w or
4m or (if older)
16y
Post #526121
Posted Monday, June 30, 2008 12:05 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:49 AM
Points: 20,682, Visits: 32,281
Peggy Rowles (6/30/2008)
The posted codes work fine for full years, but I have been trying to find good SQL code that will calculate down to days if needed (for babies). I a baby is under a couple weeks old, I need to report how many days old they are. If the baby is older than that, but under a few months old, I need to report how many weeks old they are. If the child is older than a few months, but less than a year, I have to report how many months old they are. I haven't been able yet to come up with code that will do that and display something like (as applicable):

3d or
2w or
4m or (if older)
16y


Your requirements above a slightly vague. Could you provide firmer requirements on what needs to be reported, and how you would want the data returned?




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #526143
Posted Monday, June 30, 2008 12:07 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, August 6, 2013 1:55 PM
Points: 3, Visits: 16
That worked! thanks!
Post #526144
Posted Monday, June 30, 2008 12:21 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 5, Visits: 52
Well, they're vague because healthcare is so vague. Different health providers have different needs. But let's say that under 1 month old should show age in days; between 1 month and 3 months should age in weeks; between 3 months and 1 year should show age in months. Does that make sense?
Post #526148
Posted Monday, June 30, 2008 1:21 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:49 AM
Points: 20,682, Visits: 32,281
Now, how would you like to see the result returned? If it is a single number value, how would you distinguish between 1 day, 1 week, 1 month, 1 year?




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #526180
Posted Monday, June 30, 2008 1:29 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 5, Visits: 52
1d, 1w, 1m, and 1y

thanks for any help you can give!
Post #526188
Posted Monday, June 30, 2008 2:09 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:49 AM
Points: 20,682, Visits: 32,281
Start with this and do a lot of testing. You may need to tweak it as I did not do a lot of testing so its accuracy may vary.

declare @dob datetime,
@age int,
@day datetime

set @day = '2008-12-15'
set @dob = '2007-02-28'
set @age = datediff(yy,@dob,@day) -
case when @day < dateadd(yy, datediff(yy,@dob,@day), @dob) then 1 else 0 end

select
case
when @day < dateadd(mm, 1, @dob)
then cast(datediff(dd, @dob, @day) as varchar) + 'd'
when @day < dateadd(mm, 3, @dob)
then cast(datediff(wk, @dob, @day) as varchar) + 'w'
when @day <= dateadd(yy, 1, @dob)
then cast(datediff(mm, @dob, @day) as varchar) + 'm'
else cast(datediff(yy,@dob,@day) -
case when @day < dateadd(yy, datediff(yy,@dob,@day), @dob) then 1 else 0 end as varchar) + 'y'
end




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #526212
Posted Monday, June 30, 2008 2:27 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Today @ 10:17 AM
Points: 5, Visits: 52
VERY cool! And I can easily modify it to work in my queries, and even adjust some things for different care providers' needs!

Thank you!
Post #526224
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse