Q: This question actuallyl has two parts:
1). I have table emp with fields eno(employee number),dob(date of birth). And the retirement age is 58. -In this I have to find the employee who is above the age of 51 and the number of days remaining to the retirement.
2). In a table there are four fields of number type. my question is how to find the minimum value among the four fields.
Iain Kick says: Two part question, huh? Well, here’s a two part answer…with code!
1) declare @calcdays int
declare @retiredays int
–number of days in 51 years
set @calcdays = datediff(day, dateadd(yy, -51, getdate()), getdate())
–number of days in 58 years
set @retiredays = datediff(day, dateadd(yy, -58, getdate()), getdate())
–return those between 51 and 58 and number of days left
select eno, dob, (@retiredays – datediff(day, dob, getdate())) as DaysToRetirement
from emp
where datediff(day, dob, getdate()) between @calcdays and @retiredays
2) Ugly but it works:
select
case
when num1 <= num2 and num1 <= num3 and num1 <= num4 then num1
when num2 <= num1 and num2 <= num3 and num2 <= num4 then num2
when num3 <= num1 and num3 <= num2 and num3 <= num4 then num3
when num4 <= num1 and num4 <= num2 and num4 <= num3 then num4
end as lowestnumber
from numtable