0

I want to calculate the number of days an employee has taken leave in the month of january.For that I have two date fields Leave_Start_Date and Leave_End_Date in the database as follows

Leave_Approval_Id      Leave_Start_Date           Leave_End_Date             EmpId 
    1               2013-01-25 00:00:00.000      2013-02-10 00:00:00.000      10

Here my problem is the Leave_End_Date is on february.So when i take the datedifference im getting the value as 16,but acctually i want is 6(in the month of january alone).

How can i do it ???

4

1 回答 1

1

定义您感兴趣的区间,并使用 acase将范围值或您的实际值用作 的参数datediff()

declare @FromDate datetime
declare @ToDate datetime

set @FromDate = '20130101'
set @ToDate = '20130131'

select datediff(
                day, 
                case when Leave_Start_Date < @FromDate then @FromDate else Leave_Start_Date end,
                case when Leave_End_Date >= @ToDate then @ToDate else Leave_End_Date end
               )
from ....

这将为您6提供您指定的时间间隔。

于 2013-01-15T07:57:14.977 回答