Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
amulya.j2
Starting Member
5 Posts |
Posted - 2011-01-28 : 23:19:18
|
| Hi ,This is amulya, trying to write stored procedure for the belowscenario... i m struck ..can u help me in cracking this soln.I m basically trying to find the year of pension income which is in age as now in second time Table – DobTableSL_NO DOB1 1/1/1950Table - YearTableSL_N0 REF_Year Pension_income_start_age1 12/12/2010 55 Logic : 1. First i will find my current age My current age = dob – ref_year 2. I will get the difference between current age and pension Income start ageDifference b/n my pension income age and current age = currentage – pension income start age 3. Then i will add the diiference in age with ref_year that will give me pension income start year. Year of my pension income = ref year of table 2 + diff b/n my pension income age and current ageRegards,Amulya |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-29 : 11:31:35
|
| [code]SELECT DATEADD(yy,CASE WHEN MONTH(REF_Year) < MONTH(DOB) OR ((MONTH(REF_Year) = MONTH(DOB)) AND (DAY(REF_year) < DAY(DOB))) THEN DATEDIFF(yy,DOB,REF_Year)-1 ELSE DATEDIFF(yy,DOB,REF_Year) END - Pension_income_start_age,REF_Year) AS [pension income start year]FROM Table1 t1INNER JOIN Table2 t2ON t2.SL_No = t1.SL_No[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
amulya.j2
Starting Member
5 Posts |
Posted - 2011-01-29 : 14:05:22
|
| visak, thank u soo much.. please if you dont mind can you explain me on this ? i m little confused..i guess its single select statement right ?how easy is it to do in stored procedure visaki tired something like thisCREATE FUNCTION dbo.GetAge ( @pDateOfBirth DATETIME, @pAsOfDate DATETIME )RETURNS INTASBEGIN DECLARE @vAge INT IF @pDateOfBirth >= @pAsOfDate RETURN 0 SET @vAge = DATEDIFF(YY, @pDateOfBirth, @pAsOfDate) IF MONTH(@pDateOfBirth) > MONTH(@pAsOfDate) OR (MONTH(@pDateOfBirth) = MONTH(@pAsOfDate) AND DAY(@pDateOfBirth) > DAY(@pAsOfDate)) SET @vAge = @vAge - 1 RETURN @vAgeENDGO CREATE FUNCTION dbo.GetAgeDiff(@pensionAge int, @currage int)RETURNS INTASBEGIN DECLARE @vAge INT set @vAge = @currage - @pensionAge RETURN @vAgeENDGO Create function dbo.GetPensionYear(@ageDiff int, @datetime DateTime)RETURNS INTASBEGIN Declare @refYear INT DECLARE @year INT set @refYear = year(@datetime) set @year = @refYear + @ageDiff RETURN @yearENDGO -- pension start yearselect dbo.GetPensionYear(dbo.GetAgeDiff(pension_income_Start_Age, dbo.GetAge(Table1.DATE_OF_BIRTH,table2.PLAN_DATE)),table2.PLAN_DATE) As Pension_Start_Yearfrom table1, table2 where Table1.DATE_OF_BIRTH is not nulland table2.PLAN_DATE is not null and Table1.sl_no = table2.sl_no andtable2.pension_income_start_age is not null and table2.pension_income_start_age <>0Go |
 |
|
|
amulya.j2
Starting Member
5 Posts |
Posted - 2011-01-30 : 22:19:36
|
| visak , i m getting this errorMsg 517, Level 16, State 1, Line 1Adding a value to a 'datetime' column caused an overflow. |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-31 : 00:21:38
|
| getting it as a result of the select Statement? if so which one .. the one specified to by by Visakh or any other one .. By the way if the code Specified by Visakh is correct then why you are not using it? |
 |
|
|
amulya.j2
Starting Member
5 Posts |
Posted - 2011-01-31 : 22:02:04
|
| i m getting the error from vikas code. please let me know |
 |
|
|
amulya.j2
Starting Member
5 Posts |
Posted - 2011-01-31 : 22:53:59
|
| what if age field is null |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|
|