all groups > sql server programming > october 2003 >
You're in the

sql server programming

group:

Nearest Age


Nearest Age JDP NO[at]SPAM Work
10/7/2003 9:41:23 PM
sql server programming: Here's a simple version of my code if fails just like I expected. I'm so
unclear as to my method, that everything I can think of seems stupid. I just
need a method, I can work out the script.

/*
Nearest Age
This example fails for a date of 01/07/2003 and a bday of 11/15/1960
*/
declare
@uage int
,@bday datetime
,@date datetime
,@startd datetime
,@bdate_current datetime

-- set basis date, normally this would be getdate()
set @date = '10/07/2003'

-- set the birthdate to calculate
set @bday = '04/15/1960'

-- set the birhdate to the current year
set @bdate_current = dateadd(yy,datediff(yy,@bday,@date),@bday)

if @bdate_current < @date
begin
print 'True age'
set @uage = datediff(yy,@bday,@date)
print @uage
end
if @bdate_current > @date
begin
print 'Add 1'
set @uage = datediff(yy,@bday,@date)+1
print @uage
end

TIA

JeffP....


Re: Nearest Age JDP NO[at]SPAM Work
10/8/2003 8:07:10 AM
Jacco, thanks.... I haven't time to digest the qry, but my brief testing worked
and I'm very happy now.

JeffP...

[quoted text, click to view]

Re: Nearest Age JDP NO[at]SPAM Work
10/8/2003 9:16:55 AM
Thank you. I was overwhelmed by the syntax and was so happy that it worked.

I've since re-written; here is the snipit from my trigger. Prior to this the
users would adjust the age up or down, calc the rate(s) then re-set it back. It
was a pain and relied on the user re-setting the date prior to documents going
out, needless to say doc's were going out with wrong bdays.

This is better.

declare
-- reset age to closest bday
set @uage =
case when dateadd(yy,datediff(yy,@bday,getdate()),@bday) >= getdate()
then
case when dateadd(dd,183,getdate()) >
dateadd(yy,datediff(yy,@bday,getdate()),@bday)
then datediff(yy,@bday,getdate())
else datediff(yy,@bday,getdate()) - 1
end
else
case when dateadd(dd,-183,getdate()) >
dateadd(yy,datediff(yy,@bday,getdate()),@bday)
then datediff(yy,@bday,getdate()) + 1
else datediff(yy,@bday,getdate())
end
end
-- end set age

JeffP...
P.S. I posted this solution in swink and gave thanks to Jacco....

[quoted text, click to view]

Re: Nearest Age Jacco Schalkwijk
10/8/2003 10:00:50 AM
Hi Jeff,


The following works, you can use 183 days instead of 6 months if you like

DECLARE @bday DATETIME
DECLARE @currentday DATETIME

SET @bday = '19600115'
SET @currentday = '20031101'

SELECT
CASE WHEN dateadd(yy,datediff(yy,@bday,@currentday),@bday) >= @currentday
THEN
CASE WHEN dateadd(mm, 6, @currentday) > dateadd(yy,datediff
(yy,@bday,@currentday),@bday)
THEN datediff(yy,@bday,@currentday)
ELSE datediff(yy,@bday,@currentday) - 1
END
ELSE
CASE WHEN dateadd(mm, - 6, @currentday) >
dateadd(yy,datediff(yy,@bday,@currentday),@bday)
THEN datediff(yy,@bday,@currentday) +1
ELSE datediff(yy,@bday,@currentday)
END
END

--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]

Re: Nearest Age Jacco Schalkwijk
10/8/2003 4:24:20 PM
Let me help you a bit with your digestion :-)

The first CASE checks if the birthday falls after or before the currentday
in the calendar year. If it falls after the second CASE checks if the
(adjusted) birthday falls more than 6 months after the currentday. If that
is true the closest age is the number of year boundaries (DATEDIFF) between
currentday and birthday +1, otherwise it is the number of year boundaries.
Second part works in reverse.

--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]

Re: Nearest Age Jacco Schalkwijk
10/8/2003 8:19:00 PM
Hi Jeff,

If you use that in a trigger it is probably better to create the age column
as a computed column, something like:

ALTER TABLE jeff DROP COLUMN age

ALTER TABLE jeff ADD age AS CASE WHEN
dateadd(yy,datediff(yy,bday,GETDATE()),bday) >= GETDATE()
THEN
CASE WHEN dateadd(mm, 6, GETDATE()) > dateadd(yy,datediff
(yy,bday,GETDATE()),bday)
THEN datediff(yy,bday,GETDATE())
ELSE datediff(yy,bday,GETDATE()) - 1
END
ELSE
CASE WHEN dateadd(mm, - 6, GETDATE()) >
dateadd(yy,datediff(yy,bday,GETDATE()),bday)
THEN datediff(yy,bday,GETDATE()) +1
ELSE datediff(yy,bday,GETDATE())
END
END

No trigger, no extra space to store the calculated column, and the column is
always up to date when you retrieve the data.


--
Jacco Schalkwijk
SQL Server MVP



[quoted text, click to view]

Re: Nearest Age JDP NO[at]SPAM Work
10/8/2003 10:05:33 PM
Actually I only keep the actual age and birthdate, but use the nearest age to
calculate client rates based on age for one of our carriers. The value goes
into a msg field as, 'FCL Current Rates 48' to let the underwriter know what
age was used to calc the rates, I need this age frozen for all time and if need
be could through it into an existing column.

JeffP....

[quoted text, click to view]

Re: Nearest Age Jacco Schalkwijk
10/9/2003 8:56:05 AM
I got it. In your scenario a computed column doesn't make sense and the way
you designed it is indeed the right way.

--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]
AddThis Social Bookmark Button