为什么它会返回负值?我正在传递每一个积极的价值观?但它确实返回负值?我尝试了每一个价值观,每个都是积极的,但没有回报积极的? 请帮助我,我尝试了每一件事
ALTER FUNCTION [dbo].[GetShiftTotalDurationForDays]
(
@DepartmentShiftHistory_ID int,
@FromDate DateTime,
@ToDate DateTime,
@UserID int
)
RETURNS int
AS
BEGIN
Declare @TotalShiftHolidays int
Declare @TotalGazetted int
Declare @TotalLeaves int
Declare @SumOfHolidays int
Declare @TotalDays int
--Declare @TrimmedAttendanceDate varchar(7)
Declare @TrimmedFromDate varchar(7)
Declare @TrimmedToDate varchar(7)
Declare @ShifTimeDifference int
Declare @ShiftStartTime time
Declare @ShiftEndTime time
Declare @ShiftTimeForTotalDays int
Declare @ShiftID int
set @ShiftID= (select Shift_ID From DeparmentShiftsHistory Where DepartmentShiftHistoryID=@DepartmentShiftHistory_ID)
-- Set @TrimmedAttendanceDate = (Select CONVERT(Varchar(7), Attendance.AttendanceDate, 113) from Attendance)
Set @TrimmedFromDate = (Select CONVERT(Varchar(7), @FromDate , 113))
Set @TrimmedToDate = (Select CONVERT(Varchar(7), @ToDate, 113))
Set @TotalShiftHolidays= (Select Count(*) from dbo.Attendance
where IsShiftHoliday=1 AND AttendanceDate >= @FromDate
AND AttendanceDate < dateadd(day,1,@ToDate))
--AND User_ID= @UserID )
Set @TotalGazetted = (Select Count(*) from dbo.Attendance
where IsGazettedHoliday=1
AND datepart(year,@FromDate) = datepart(year,AttendanceDate))
-- @TrimmedAttendanceDate >= @TrimmedFromDate
--AND @TrimmedAttendanceDate <= @TrimmedToDate
-- AND User_ID= @UserID)
Set @TotalLeaves= (Select Count(*) from dbo.Attendance
where IsLeaveHoliday=1 AND AttendanceDate >= @FromDate
AND AttendanceDate <dateadd(day,1,@ToDate) AND User_ID= @UserID )
Set @SumOfHolidays = (@TotalShiftHolidays + @TotalGazetted + @TotalLeaves)
Set @TotalDays = (Select DATEDIFF(dd, @FromDate, @ToDate)) - @SumOfHolidays --Exlcuding holidays
Set @ShiftStartTime = (Select Cast(DeparmentShiftsHistory.StartTime as time) as StartTime from DeparmentShiftsHistory
where DepartmentShiftHistoryID=@DepartmentShiftHistory_ID)
--DeparmentShiftsHistory.Shift_ID= @ShiftID )
Set @ShiftEndTime = (Select Cast(DeparmentShiftsHistory.EndTime as time) as EndTime from DeparmentShiftsHistory
where DepartmentShiftHistoryID=@DepartmentShiftHistory_ID)
-- DeparmentShiftsHistory.Shift_ID= @ShiftID )
Set @ShifTimeDifference = (Select DATEDIFF(minute,@ShiftEndTime,@ShiftStartTime)) --Returns time difference in Minutes
Set @ShiftTimeForTotalDays = @ShifTimeDifference * @TotalDays
RETURN @ShiftTimeForTotalDays -- //Total Shift Timmings BetWeennn Dates in Minutes
END
答案 0 :(得分:0)
DATEDIFF将返回正差或负值,具体取决于参数的顺序:
Declare @a datetime='20140101'
Declare @b datetime='20120101'
Select DATEDIFF(MINUTE,@a,@b),DATEDIFF(MINUTE,@b,@a)
要获得正值,第一个参数必须是次要参数。