I’m facing one problem from last 5 days. I need your help. I’m creating sp to calculate Gratuity of employee, but I’m unsle to calculate the @DayCount due to leap year. Can you please see the below stored procedure,

ALTER PROCEDURE [Hrms].[Usp_CalculateGratuity]

(

@GratuityDate Datetime,

@EmployeeID varchar(20),

–@EMPLOYEE_NUMBER VARCHAR(20),

@Gratuity INT OUTPUT

)

As

BEGIN

— Get salary drawn

DECLARE @LastSalary DECIMAL(10,4)

SELECT @LastSalary = BasicPay from ALLSec.dbo.tblAllSecLeaveSync where EmployeeID=@EmployeeID

— Get First working date

DECLARE @HireDate DATETIME

SELECT TOP 1 @HireDate = First_Hire_date FROM Hrms.dbo.Hrms_Employee_Detail where EMPLOYEE_NUMBER = @EmployeeID

ORDER BY Hrms_Employee_Detail.First_Hire_date DESC

— Get last working date

DECLARE @LWD DATETIME

SELECT @LWD= [Approved LWD] FROM hrms.tblEmployeeDetails WHERE EmployeeNumber=@EmployeeID

— Get total completed years

DECLARE @CompletedYears INT

SELECT @CompletedYears = DATEDIFF(YEAR,@HireDate,@LWD)

— Calculate days in years

DECLARE @DayCount INT

SET @DayCount = @CompletedYears*365

— Get total days

DECLARE @CompletedDays INT

SET @CompletedDays = DATEDIFF(DAY,@HireDate,@LWD) – @DayCount

— Get total Months

DECLARE @TotalMonths INT

SET @TotalMonths = (@CompletedYears * 12)- DATEDIFF(MONTH,@HireDate,@LWD)

— when number of years >= 4 years and days >= 239 days then return calculated Gratuity otherwise Gratuity = 0

IF(@CompletedYears >= 4)

IF ((@CompletedYears = 4) AND (@CompletedDays >= 239))

BEGIN

— If months >= 6 then total years should be increment by 1

— Need not this condition here

IF (@TotalMonths >= 6)

BEGIN

SET @CompletedYears = @CompletedYears + 1

END

— Finally calculate the Gratuity of employee

SET @Gratuity = 15/26 * @LastSalary * @CompletedYears

END

— If completed years > 4

ELSE

BEGIN

— If months >= 6 then total years should be increment by 1

IF (@TotalMonths >= 6)

BEGIN

SET @CompletedYears = @CompletedYears + 1

END

— Finally calculate the Gratuity of employee

SET @Gratuity = 15/26 * @LastSalary * @CompletedYears

END

ELSE

BEGIN

SET @Gratuity = 0

END

— Get Calculated Gratuity

SELECT @Gratuity

END

LikeLike

]]>LikeLike

]]>LikeLike

]]>http://beyondrelational.com/modules/2/blogs/70/posts/10934/different-ways-to-find-out-a-leap-year.aspx

LikeLike

]]>CREATE FUNCTION dbo.IsLeapYear (@year INT)

RETURNS INT

AS

BEGIN

RETURN ISDATE(’02/29/’+ Cast(@year AS varchar(4)))

END

GO

SELECT dbo.IsLeapYear(2012)

LikeLike

]]>LikeLike

]]>LikeLike

]]>LikeLike

]]>Admittedly the next adjustment year is 2100 so many of us will not actually care about this but fact remains that we cannot just check if the year is divisible by 4.

This is why MS saw fit to put the function in to SQL so that developers do not have to worry about it. I think we should avoid re-inventing the wheel and check a date against what Microsoft has done for us (even in 2008 the functionality exists as it knows if there are 29 days if you ask it that question directly) hence I think it works to take one off 1st march and test if its 29 in SQL Server 2008 and use the function Pinal spoke of for 2012.

Personally I would always recommend using a date (calendar) table in your database ratherthan a date calculation as you allow not oly for leap years but also all other special dates, company financial year etc.

Check out this excerpt from wikipedia page

http://en.wikipedia.org/wiki/Leap_year

“most years that are evenly divisible by 4 are leap years…”

“…Some exceptions to this rule are required since the duration of a solar year is slightly less than 365.25 days. Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years. For example, 1600 and 2000 were leap years, but 1700, 1800 and 1900 were not. Similarly, 2100, 2200, 2300, 2500, 2600, 2700, 2900 and 3000 will not be leap years, but 2400 and 2800 will be.”

LikeLike

]]>(@date smalldatetime) returns bit

as

begin

declare @r bit

set @r = case year(@date)%4 when 0 then 1 else 0 end

return @r

end

select dbo.fncLeapYear (getdate())

LikeLike

]]>