I want to return the following values as shown below:
fechaActual Clave Usuario Depto entrada salida sumaHoras
201712 307 USER1 TTT 06:00:04 14:04:12 8
The query must take the current date of the system and from this should do the filtering however; the result is different from the above query, on the one hand the sum is correct but does not show the correct entry and exit, the output that shows them is taking part of what is defined in:
SELECT @Hoy1 = CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE(), 112))+'00:00:00'
SELECT @Hoy2 = CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE(), 112))+'23:59:59'.
And also because the query is defined in this way:
CONVERT (VARCHAR(20), @Hoy1,108) fechaEntrada,
CONVERT (VARCHAR(20), @Hoy2,108) fechaSalida,
Query:
DECLARE @Hoy DATETIME
DECLARE @Hoy1 DATETIME
DECLARE @Hoy2 DATETIME
SELECT @Hoy = CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE(), 112))
SELECT @Hoy1 = CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE(), 112))+'00:00:00'
SELECT @Hoy2 = CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE(), 112))+'23:59:59'
select c.CodeCompany,
CONVERT(char(6), @Hoy, 112) AS fechaActual,
a.idClave,
c.nombre + ' ' + c.apellidos as Nombre,
d.Descripcion as Depto,
CONVERT (VARCHAR(20), @Hoy1,108) fechaEntrada,
CONVERT (VARCHAR(20), @Hoy2,108) fechaSalida,
DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24 AS horasAlDia
from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
JOIN tblDepto d ON c.fkDepto=d.idDepto
where
fechaEntrada BETWEEN CONVERT(DATETIME, @Hoy1,103) and
CONVERT(DATETIME, @Hoy2,103)and fkStatus !=1
Result of the query
fechaActual Clave Usuario Depto entrada salida sumaHoras
201712 10120 USER1 TTT 00:00:00 23:59:59 8
Desired result:
fechaActual Clave Usuario Depto entrada salida sumaHoras 201712 307 USER1 TTT 06:00:04 14:04:12 8
Incorrect result:
fechaActual Clave Usuario Depto entrada salida sumaHoras
201712 10120 USER1 TTT 00:00:00 23:59:59 8