Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

return 0 when value is greater than mentioned

$
0
0

i wrote this query to find difference between TOTALREQUIREDHOURS and TOTALWORKDEDHOURS, it works e.g. 192-186:05= 5:55 but problem is if TotalWorkedHours are greater than totalRequiredHours then it returns result in negative so i want if RESULT is negatived then it should return 0, 

So this code should only work in case when TotalWorkedHours < TotalRequiredHours and should return 0 in case of TotalWorkedHours < TotalRequiredHours or TotalWorkedHours = TotalRequiredHours 

CODE:

USE [a1]
GO
/****** Object:  UserDefinedFunction [dbo].[GetPayDeducted]    Script Date: 2014-02-25 11:39:56 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[GetPayDeducted] 
(
	@emplID int,
	@month VARCHAR(50) = NULL
)
RETURNS int
AS
BEGIN
	Declare @StartDate Date,  @EndDate Date, @mydate date, 
	        @TotalDays int, @TotalHours int, @BasicSalary float, 
			@BSalaryHour int, @TotalSalaryDeducted float, @hms varchar(100),
			@hrs int, @mts int, @TotalHoursDeducted float,
			@hrsDeducted Varchar(11)
	   
	    Set @hrsDeducted = dbo.GetHoursDeducted(@emplID, @month)
		set @hms = @hrsDeducted
        Set @hrs = LEFT(@hms,charindex(':',@hms)-1)
	    set @mts=RIGHT(@hms,len(@hms)-charindex(':',@hms))
        set @TotalHoursDeducted = @hrs + (@mts/60.0)
		Set @mydate = GETUTCDATE()
		Set @StartDate = (SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),@mydate),101))
		Set @EndDate = (SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)),101))
		Set @TotalDays =((DATEDIFF(dd, @StartDate, @EndDate) + 1)
		  -(DATEDIFF(wk, @StartDate, @EndDate) * 1)
		  -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)) 
		Set @TotalHours  = (@TotalDays * 8)
		Set @BasicSalary = (Select BasicSalary from HrEmployee where EmplID=@emplID)
		--Set @TotalHoursWorked = (Select OverallTime from MonthlyRecord where EmplID = @emplID AND Month = @month )
		Set @BSalaryHour = @BasicSalary / @TotalHours
		Set @TotalSalaryDeducted = @BSalaryHour * @TotalHoursDeducted
		--------------------------------------------------------------------------------------
	-- Return the result of the function
	RETURN  @TotalSalaryDeducted

END


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>