Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

Following code is working fine

SQL
SELECT EmpId ,CAST(InTime AS DATE) as workingday ,EmpName,RIGHT('0' + CONVERT(varchar, FLOOR(SUM( DATEDIFF( MI, InTime, OutTime)/60.0))) , 2) + ':' + RIGHT('0' + CONVERT(varchar, SUM( DATEDIFF( MI, InTime, OutTime))%60), 2) AS [total hour(s)],
    convert(varchar(5), max(OutTime)-MIN(InTime),108) as Emp_Intime,dstatus=
    (CASE
     WHEN CONVERT(TIME,MIN(InTime),108)>'08:35' and CONVERT(TIME,MIN(InTime),108) <'11:00'
     THEN 'L'
      WHEN CONVERT(TIME,MIN(InTime),108)>'11:00'
      THEN 'halfday'
      else 'right' end ),CAST( RIGHT('0' + CONVERT(varchar, FLOOR(SUM( DATEDIFF( MI, InTime, OutTime)/60.0))) , 2) + ':' + RIGHT('0' + CONVERT(varchar, SUM( DATEDIFF( MI, InTime, OutTime))%60), 2)-CAST( max(OutTime)-MIN(InTime) AS DATETIME) AS TIME) Extratime
    FROM Baiju.dbo.HbaEmp
    GROUP BY EmpId , EmpName, CAST(InTime AS DATE)


empid workingday   Empname     total_hour(s)     Emp_Intime    status        Extratime

    2500    2014-01-01    Arunkumar    10:10                 09:40            L               00:30:00.0000000
    2502    2014-01-01    Arsh             07:48                  06:48          halfday      01:00:00.0000000
    2503    2014-01-01    RaJkumar    08:40                    08:10           L              00:30:00.0000000
    2504    2014-01-01    hari              NULL                 NULL           right           NULL
    2504    2014-01-01    Sini              08:55                    08:55           L             00:00:00.0000000


my requirement is to find overtime.

add a field Overtime .and condition is (Emp_Intime-00:45)-Extratime and check if the value is above 8 hour the overtime is value-8 else overtime is 0

for example first record of above should be

empid workingday   Empname     total_hour(s)     Emp_Intime    status        Extratime                overtime

    2500    2014-01-01    Arunkumar    10:10                 09:40            L               00:30:00.0000000   25


ie (09:40-45)-30.

ie 08:55-30=08:25

overtime is 25 minutes

How it is possible

Regards

Baiju
Posted
Updated 18-Feb-14 1:51am
v2

1 solution

Normally working hours of an organisation is 8 hrs per day so I have used working hrs as 8 and deduct from total hours so it shows Overtime.

SQL
SELECT EmpId ,CAST(InTime AS DATE) as workingday ,EmpName,RIGHT('0' + CONVERT(varchar, FLOOR(SUM( DATEDIFF( MI, InTime, OutTime)/60.0))) , 2) + ':' + RIGHT('0' + CONVERT(varchar, SUM( DATEDIFF( MI, InTime, OutTime))%60), 2) AS [total hour(s)],
    convert(varchar(5), '08:00',108) as Working_hours,dstatus=
    (CASE
     WHEN CONVERT(TIME,MIN(InTime),108)>'08:35' and CONVERT(TIME,MIN(InTime),108) <'11:00'
     THEN 'Late'
      WHEN CONVERT(TIME,MIN(InTime),108)>'11:00'
      THEN 'Halfday'
      WHEN CONVERT(TIME,MIN(OutTime),108)>'16:30'
      THEN 'OT'
      else 'Full Day' end ),CAST( RIGHT('0' + CONVERT(varchar, FLOOR(SUM( DATEDIFF( MI, InTime, OutTime)/60.0))) , 2) + ':' + RIGHT('0' + CONVERT(varchar, SUM( DATEDIFF( MI, InTime, OutTime))%60), 2)-CAST( '08:00' AS DATETIME) AS TIME) Extratime
    FROM HbaEmp
    GROUP BY EmpId , EmpName, CAST(InTime AS DATE)




CSS
1   2014-02-21  AAA 09:00   08:00   OT  01:00:00.0000000
2   2014-02-21  BBB 08:00   08:00   Full Day    00:00:00.0000000
3   2014-02-21  CCC 08:35   08:00   OT  00:35:00.0000000
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900