Tengo la siguiente consulta y en el calculo de varias columnas saco la diferencia en minutos entre dos horas dadas.
SELECT KE.HRZMX(CONCAT(CONCAT(CONCAT(CONCAT(SUBSTRING(CAST(SDDP AS varchar),1,4),'-'), CONCAT(SUBSTRING(CAST(SDDP AS varchar),5,2),'-')),
CONCAT(SUBSTRING(CAST(SDDP AS varchar),7,2),' '), CONCAT(SUBSTRING(CAST(STD AS varchar),1,2),':')), SUBSTRING(CAST(STD AS varchar),3,2))) FechaProg,
KE.HRZMX(CONCAT(CONCAT(CONCAT(CONCAT(SUBSTRING(CAST(DATDEPP AS varchar),1,4),'-'), CONCAT(SUBSTRING(CAST(DATDEPP AS varchar),5,2),'-')),
CONCAT(SUBSTRING(CAST(DATDEPP AS varchar),7,2),' '), CONCAT(SUBSTRING(CAST(HRDEP AS varchar),1,2),':')), SUBSTRING(CAST(HRDEP AS varchar),3,2))) FechaReal,
CODAVION, NROVOL, CASE WHEN APD = 'PNG' THEN 'PDS' WHEN APD = 'TNY' THEN 'TPQ' WHEN APD = 'COL' THEN 'CLQ' WHEN APD = 'TMN' THEN 'TSL' ELSE APD END AS Orígen,
CASE WHEN APA = 'PNG' THEN 'PDS' WHEN APA = 'TNY' THEN 'TPQ' WHEN APA = 'COL' THEN 'CLQ' WHEN APA = 'TMN' THEN 'TSL' ELSE APA END AS Destino,
CONCAT(CONCAT(SUBSTRING(CAST(STD AS varchar),1,2),':'), SUBSTRING(CAST(STD AS varchar),3,2)) HoraSalPrev,
CONCAT(CONCAT(SUBSTRING(CAST(HRDEP AS varchar),1,2),':'), SUBSTRING(CAST(HRDEP AS varchar),3,2)) HoraSalReal,
CONCAT(CONCAT(SUBSTRING(CAST(HRDEPABOP AS varchar),1,2),':'), SUBSTRING(CAST(HRDEPABOP AS varchar),3,2)) HoraDespegue,
CONCAT(CONCAT(SUBSTRING(CAST(HRARRABOP AS varchar),1,2),':'), SUBSTRING(CAST(HRARRABOP AS varchar),3,2)) HoraAterrizaje,
CONCAT(CONCAT(SUBSTRING(CAST(HRARR AS varchar),1,2),':'), SUBSTRING(CAST(HRARR AS varchar),3,2)) HoraLlegReal,
CONCAT(CONCAT(SUBSTRING(CAST(STA AS varchar),1,2),':'), SUBSTRING(CAST(STA AS varchar),3,2)) HoraLlegPrev,
PAXEMB1 Pasajeros, PAXMAX Capacidad, ISNULL(PAXEMB1,0) / PAXMAX LF,
DATEDIFF(MINUTE,CONCAT(CONCAT(SUBSTRING(CAST(STD AS varchar),1,2),':'), SUBSTRING(CAST(STD AS varchar),3,2)), CONCAT(CONCAT(SUBSTRING(CAST(HRDEP AS varchar),1,2),':'), SUBSTRING(CAST(HRDEP AS varchar),3,2))) DemoraSalida,
DATEDIFF(MINUTE,CONCAT(CONCAT(SUBSTRING(CAST(STA AS varchar),1,2),':'), SUBSTRING(CAST(STA AS varchar),3,2)), CONCAT(CONCAT(SUBSTRING(CAST(HRARR AS varchar),1,2),':'), SUBSTRING(CAST(HRARR AS varchar),3,2))) DemoraLlegada,
IIF(DATEDIFF(MINUTE,CONCAT(CONCAT(SUBSTRING(CAST(STD AS varchar),1,2),':'), SUBSTRING(CAST(STD AS varchar),3,2)), CONCAT(CONCAT(SUBSTRING(CAST(HRDEP AS varchar),1,2),':'), SUBSTRING(CAST(HRDEP AS varchar),3,2)))>0,'Demorado', 'A Tiempo') TipoDemoraSalida,
IIF(DATEDIFF(MINUTE,CONCAT(CONCAT(SUBSTRING(CAST(STA AS varchar),1,2),':'), SUBSTRING(CAST(STA AS varchar),3,2)), CONCAT(CONCAT(SUBSTRING(CAST(HRARR AS varchar),1,2),':'), SUBSTRING(CAST(HRARR AS varchar),3,2)))>0,'Demorado', 'A Tiempo') TipoDemoraLlegada,
CASE WHEN DATEDIFF(MINUTE,CONCAT(CONCAT(SUBSTRING(CAST(STA AS varchar),1,2),':'), SUBSTRING(CAST(STA AS varchar),3,2)), CONCAT(CONCAT(SUBSTRING(CAST(HRARR AS varchar),1,2),':'), SUBSTRING(CAST(HRARR AS varchar),3,2))) BETWEEN 5 AND 9 THEN 'ATD+5'
WHEN DATEDIFF(MINUTE,CONCAT(CONCAT(SUBSTRING(CAST(STA AS varchar),1,2),':'), SUBSTRING(CAST(STA AS varchar),3,2)), CONCAT(CONCAT(SUBSTRING(CAST(HRARR AS varchar),1,2),':'), SUBSTRING(CAST(HRARR AS varchar),3,2))) BETWEEN 10 AND 14 THEN 'ATD+10'
WHEN DATEDIFF(MINUTE,CONCAT(CONCAT(SUBSTRING(CAST(STA AS varchar),1,2),':'), SUBSTRING(CAST(STA AS varchar),3,2)), CONCAT(CONCAT(SUBSTRING(CAST(HRARR AS varchar),1,2),':'), SUBSTRING(CAST(HRARR AS varchar),3,2))) > 15 THEN 'ATD+15' ELSE 'ATD+0' END AS ATD,
DATEPART(HOUR,KE.HRZMX(CONCAT(CONCAT(SUBSTRING(CAST(STD AS varchar),1,2),':'), SUBSTRING(CAST(STD AS varchar),3,2)))) HoraSalPrev
FROM KE.Vuelos2
En la teoría funciona bien con excepción de los siguientes ejemplos:
La diferencia debería de ser solo 60 minutos ya que se demoro una hora pero lo está tomando como si fuera otro día y coloca una diferencia de 1380 minutos.