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....
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] "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message news:uR1eoqXjDHA.2140@TK2MSFTNGP09.phx.gbl... > 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 > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > news:eeFHfWVjDHA.644@TK2MSFTNGP11.phx.gbl... > > 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.... > > > > > > > >
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] "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message news:eIyx4%23ajDHA.2512@TK2MSFTNGP09.phx.gbl... > 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 > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > news:u4xbH0ajDHA.708@TK2MSFTNGP10.phx.gbl... > > Jacco, thanks.... I haven't time to digest the qry, but my brief testing > worked > > and I'm very happy now. > > > > JeffP... > > > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > > news:uR1eoqXjDHA.2140@TK2MSFTNGP09.phx.gbl... > > > 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 > > > > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > > news:eeFHfWVjDHA.644@TK2MSFTNGP11.phx.gbl... > > > > 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.... > > > > > > > > > > > > > > > > > > > > > > > >
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] "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message news:eeFHfWVjDHA.644@TK2MSFTNGP11.phx.gbl... > 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.... > > >
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] "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message news:u4xbH0ajDHA.708@TK2MSFTNGP10.phx.gbl... > Jacco, thanks.... I haven't time to digest the qry, but my brief testing worked > and I'm very happy now. > > JeffP... > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > news:uR1eoqXjDHA.2140@TK2MSFTNGP09.phx.gbl... > > 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 > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > news:eeFHfWVjDHA.644@TK2MSFTNGP11.phx.gbl... > > > 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.... > > > > > > > > > > > > > > >
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] "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message news:%23Z7rFbbjDHA.3612@TK2MSFTNGP11.phx.gbl... > 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.... > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > news:eIyx4%23ajDHA.2512@TK2MSFTNGP09.phx.gbl... > > 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 > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > news:u4xbH0ajDHA.708@TK2MSFTNGP10.phx.gbl... > > > Jacco, thanks.... I haven't time to digest the qry, but my brief testing > > worked > > > and I'm very happy now. > > > > > > JeffP... > > > > > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > > > news:uR1eoqXjDHA.2140@TK2MSFTNGP09.phx.gbl... > > > > 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 > > > > > > > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > > > news:eeFHfWVjDHA.644@TK2MSFTNGP11.phx.gbl... > > > > > 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.... > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
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] "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message news:eFkwbBdjDHA.976@tk2msftngp13.phx.gbl... > 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 > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > news:%23Z7rFbbjDHA.3612@TK2MSFTNGP11.phx.gbl... > > 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.... > > > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > > news:eIyx4%23ajDHA.2512@TK2MSFTNGP09.phx.gbl... > > > 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 > > > > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > > news:u4xbH0ajDHA.708@TK2MSFTNGP10.phx.gbl... > > > > Jacco, thanks.... I haven't time to digest the qry, but my brief > testing > > > worked > > > > and I'm very happy now. > > > > > > > > JeffP... > > > > > > > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > > > > news:uR1eoqXjDHA.2140@TK2MSFTNGP09.phx.gbl... > > > > > 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 > > > > > > > > > > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > > > > news:eeFHfWVjDHA.644@TK2MSFTNGP11.phx.gbl... > > > > > > 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.... > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
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] "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message news:e%23eLoIijDHA.3732@tk2msftngp13.phx.gbl... > 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.... > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > news:eFkwbBdjDHA.976@tk2msftngp13.phx.gbl... > > 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 > > > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > news:%23Z7rFbbjDHA.3612@TK2MSFTNGP11.phx.gbl... > > > 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.... > > > > > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > > > news:eIyx4%23ajDHA.2512@TK2MSFTNGP09.phx.gbl... > > > > 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 > > > > > > > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > > > news:u4xbH0ajDHA.708@TK2MSFTNGP10.phx.gbl... > > > > > Jacco, thanks.... I haven't time to digest the qry, but my brief > > testing > > > > worked > > > > > and I'm very happy now. > > > > > > > > > > JeffP... > > > > > > > > > > "Jacco Schalkwijk" <NOSPAMjaccos@eurostop.co.uk> wrote in message > > > > > news:uR1eoqXjDHA.2140@TK2MSFTNGP09.phx.gbl... > > > > > > 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 > > > > > > > > > > > > > > > > > > "JDP@Work" <JPGMTNoSpam@sbcglobal.net> wrote in message > > > > > > news:eeFHfWVjDHA.644@TK2MSFTNGP11.phx.gbl... > > > > > > > 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
Don't see what you're looking for? Try a search.
|