Script to get failed jobs

Script to get failed jobs
Below is a TSQL query to get failed jobs last hour.
if you want failed jobs for whole day then comment "AND Run_time >= @Ctime" line
DECLARE @Cdate INT
DECLARE @Ctime INT

SET @Cdate = CONVERT(CHAR(8), (SELECT
                                 Dateadd (DAY, ( 0 ), Getdate())), 112)
SET @Ctime = Cast(Replace(CONVERT(CHAR(8), (SELECT
                                              Dateadd (HOUR, -1, Getdate())), 114), ':', '') AS INT);

WITH cte
     AS (SELECT DISTINCT
           Row_number()
             OVER(
               partition BY T2.name
               ORDER BY run_time DESC) AS rnk,
           Substring(T2.Name, 1, 40)   AS 'Job Name',
           T1.Step_id                  AS 'Step_id',
           T1.Step_name                AS 'Step Name',
           T1.Message                  AS 'Error Message',
           Run_time
         FROM
           MSDB.DBO.Sysjobhistory T1
         JOIN   MSDB.DBO.Sysjobs T2
           ON T1.Job_id = T2.Job_id
         WHERE
          T1.Run_status NOT IN ( 1, 4 )
          AND T1.Step_id != 0
          AND Run_date     = @Cdate
          AND Run_time >= @Ctime)
SELECT
  *
FROM
  cte
WHERE
  rnk = 1 

Regards, Nirav gajjar