Get List of all failed SSIS package on a particuar date - sql

Hi everyone is there any way we can get list of all failed packages on any particular date , can we do this with a SQL query ?
We are using SSIS 2017 .

Assuming the package is deployed to SSISDB and ran from the catalog, query the SSISDB.CATALOG.EXECUTIONS DMV for executions with a status of 4. Packages with a status of 4 resulted in failure, as specified in the documentation. Since the start_time column is of the datetimeoffset date type and I'm assuming you only want to query by the date, not time, that a package failed, this column is cast to a date below in order for it to default to midnight.
SELECT EXECUTION_ID
,FOLDER_NAME
,PROJECT_NAME
,PACKAGE_NAME
,REFERENCE_ID
,REFERENCE_TYPE
,ENVIRONMENT_FOLDER_NAME
,ENVIRONMENT_NAME
,[OBJECT_ID]
,[STATUS]
,START_TIME
,END_TIME
,CALLER_SID
,CALLER_NAME
,SERVER_NAME
,MACHINE_NAME
FROM SSISDB.CATALOG.EXECUTIONS
--4 for failed packages
WHERE [STATUS] = 4 AND CAST(START_TIME AS DATE) = '2019-01-01'

Hi try this sql query for sql agents jobs Link:
SELECT sj.name,
sh.run_date,
sh.step_name,
STUFF(STUFF(RIGHT(REPLICATE('0', 6) + CAST(sh.run_time as varchar(6)), 6), 3, 0, ':'), 6, 0, ':') 'run_time',
STUFF(STUFF(STUFF(RIGHT(REPLICATE('0', 8) + CAST(sh.run_duration as varchar(8)), 8), 3, 0, ':'), 6, 0, ':'), 9, 0, ':') 'run_duration (DD:HH:MM:SS) ',
sh.run_status
FROM msdb.dbo.sysjobs sj
JOIN msdb.dbo.sysjobhistory sh
ON sj.job_id = sh.job_id
WHERE sh.run_status = 0
AND sh.run_date = '20190122'

Related

Stripping date from string, casting to date and checking if equal to today's date

I have a character field representing a date in the following format:
'yyyyMMdd-000000'
I'm not completely sure what the 0's represent, but I'm trying to strip those off then check if the date is the same as today's date:
SELECT
acctnum,
acctname
FROM
[Server].[dbo].[Table1]
Where
CAST(LEFT(myDate,8) AS DATE) = CAST(GetDate() as Date)
When executing that statement I get this error:
Conversion failed when converting date and/or time from character string.
What am I doing wrong and how could I fix this?
Use try_cast() instead:
Where TRY_CAST(LEFT(myDate, 8) AS DATE) = CAST(GetDate() as Date)
Then, you can find the bad data using:
select myDate
from [Server].[dbo].[Table1]
Where TRY_CAST(LEFT(myDate, 8) AS DATE) is null;
You can attempt to find the bad data with something like:
select myDate
from [Server].[dbo].[Table1]
where mydate not like '[12][90][0-9][01][0-9][0-9][1-3][0-9]%'
This doesn't find all bad examples, but if something is glaring it will pop.
declare #table1 table
(
acctnum int,
acctname varchar(10),
myDate varchar(20)
)
insert into #table1(acctnum, acctname, myDate)
values(1, 'A', '20200401-000000'),
(2, 'B', convert(varchar(20), getdate(), 112) + '-000000'),
(3, 'C', convert(varchar(20), getdate(), 112) + '-000000'),
(4, 'D', 'abcd-0000');
select *
from #table1;
select *, case isdate(stuffdate) when 1 then cast(stuffdate as date) end
from
(
select *, stuff(stuff(stuff(myDate, 14, 0, ':'), 12, 0, ':'), 9, 1, ' ') as stuffdate
from #table1
where myDate like convert(varchar(20), getdate(), 112)+'%'
) as t;
select *
from
(
select *, stuff(stuff(stuff(myDate, 14, 0, ':'), 12, 0, ':'), 9, 1, ' ') as stuffdate
from #table1
) as t
where case isdate(stuffdate) when 1 then cast(stuffdate as date) end = convert(varchar(20), getdate(), 112);

Varchar to Datetime in TSQL

How can I convert date and time value stored like
20200406151341
to DATETIME value 2020/04/06 15:31:41 (YYYY/MM/DD HH:MM:SS.000)? I am unable to find suitable CONVERT() format and the only way so far is to parse the VARCHAR like below.
select dateadd(second, cast(substring('20200406151341',13,2) as int),dateadd(minute, cast(substring('20200406151341',11,2) as int), dateadd(hour,cast(substring('20200406151341',9,2) as int),convert(datetime, left('20200406151341',8), 112)))).
It works yet it's hard to read and understand especially when I have to use it within SELECT statement multiple times.
Also I am surprised query with above conversions is as fast the one with dates stored directly in DATETIME format. Does MSSQL server uses some kind of cache so it does have to do the conversion only once per row?
I use MSSQL Server 2016.
I don't think there is a built in, simple way to do this.
You don't have to go to seconds to do this. You can easily convert the first 8 characters to a date. With some string manipulation, you can convert the last six to a time -- and then add time (as datetime values):
select convert(datetime, left(dt, 8)) + convert(datetime, stuff(stuff(right(dt, 6), 5, 0, ':'), 3, 0, ':'))
from (values ('20200406151341')) v(dt);
You can also use arithmetic rather than 3 dateadd()s:
select dateadd(second,
right(dt, 2) + 60*substring(dt, 11, 2) + 60*60*substring(dt, 9, 2),
convert(datetime, left(dt, 8)))
from (values ('20200406151341')) v(dt)
Note: This uses implicit conversion from a string to an integer (as does your version).
You can use stuff() :
select convert(datetime,
stuff(stuff(stuff(stuff(col, 9, 0, ' '), 10, 0, ''), 12, 0, ':'), 15, 0, ':'
)
Let's throw in a couple more options to the mix:
DECLARE #StrDate varchar(14) = '20200406151341'
SELECT DATETIMEFROMPARTS(
LEFT(#StrDate, 4), -- year
SUBSTRING(#StrDate, 5, 2), -- month
SUBSTRING(#StrDate, 7, 2), -- day
SUBSTRING(#StrDate, 9, 2), -- hour
SUBSTRING(#StrDate, 11, 2), -- minute
SUBSTRING(#StrDate, 13, 2), -- second
0 -- millisecond
) As [Using DateTimeFromParst],
CONVERT(DateTime, LEFT(#StrDate, 8), 112) + -- Date
CONVERT(DateTime, STUFF(STUFF(RIGHT(#StrDate, 6), 5, 0, ':'), 3, 0, ':'), 114) -- Time
As [Using convert and stuff]
Results:
Using DateTimeFromParst Using convert and stuff
2020-04-06 15:13:41 2020-04-06 15:13:41
I suggest using try_convert() or try_cast() in case your strings are invalid, and if invalid they will return NULL instead of raising an error. Refer to the links for further detail on these functions.
declare #val varchar(20)
set #val = '20200416151341'
select try_convert(datetime,
stuff(stuff(stuff(stuff(#val, 9, 0, ' '), 10, 0, ''), 12, 0, ':'), 15, 0, ':')
);
select try_cast(
stuff(stuff(stuff(stuff(#val, 9, 0, ' '), 10, 0, ''), 12, 0, ':'), 15, 0, ':')
as datetime);

Find error in jobsteps of all jobs that ran over night [duplicate]

I want write a query to get the last 24 hours worth of job record from the "msdb.dbo.sysjobhistory" table, but I can't get because I get the "run_date" and "run_time" columns are returned as a number. How can I convert the "run_date" and "run_time" columns into a datetime variable, and use this to get the last 24 hour job history?
Check out this post - it shows how to "decode" those run_date columns from sysjobhistory.
You should be able to get the entries from the last 24 hours with a query something like this:
SELECT
j.name as JobName,
LastRunDateTime =
CONVERT(DATETIME, CONVERT(CHAR(8), run_date, 112) + ' '
+ STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), run_time), 6), 5, 0, ':'), 3, 0, ':'), 121)
FROM
msdb..sysjobs j
INNER JOIN
msdb..sysjobhistory jh ON j.job_id = jh.job_id
WHERE
CONVERT(DATETIME, CONVERT(CHAR(8), run_date, 112) + ' '
+ STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), run_time), 6), 5, 0, ':'), 3, 0, ':'), 121) > DATEADD(HOUR, -24, GETDATE())
For databases after 2000, there is a function in the msdb database you can call that will return datetime:
msdb.dbo.agent_datetime(run_date, run_time) as 'RunDateTime'
If you are using sql 2000, you can copy the source of that function from a later version and create it in your instance of 2000. I wish I could take credit for all of this, but I originally found it here: mssqltips.com
A co-worker of mine pointed out that the other given solutions only find jobs that we're started 24 hours ago (or less), not jobs that were completed 24 hours ago (or less). He proposed something along the lines of the following to location completed jobs:
SELECT j.name AS JobName
,LastCompletedDateTime = DATEADD(day, (run_duration / 240000), CONVERT(DATETIME, msdb.dbo.agent_datetime(run_date, run_time))) +
STUFF(STUFF(REPLACE(STR((run_duration % 240000), 7, 0), ' ', '0'), 4, 0, ':'), 7, 0, ':')
FROM msdb..sysjobs j
INNER JOIN msdb..sysjobhistory jh ON j.job_id = jh.job_id
WHERE DATEADD(day, (run_duration / 240000), CONVERT(DATETIME, msdb.dbo.agent_datetime(run_date, run_time))) +
STUFF(STUFF(REPLACE(STR((run_duration % 240000), 7, 0), ' ', '0'), 4, 0, ':'), 7, 0, ':') > DATEADD(HOUR, - 24, GETDATE())
I believe part of this comes from a blog, but we cannot locate it, when I do I'll link that as well...

How to convert String to Datetime including time part in SQL [duplicate]

This question already has answers here:
How to convert datetime string without delimiters in SQL Server as datetime?
(6 answers)
Closed 6 years ago.
I want to convert below string to DateTime in SQL.
20140601152943767
I know convert(date,'20140601152943767') this but I want time part also.
Above function only returns me Date part.
Thanks in advance.
I would use following solution:
SELECT CONVERT(DATETIME, STUFF(STUFF(STUFF(STUFF('20140601152943767', 9, 0, ' '), 12, 0, ':') , 15, 0, ':'), 18, 0, '.'))
Note #0: All those STUFF calls will convert source strings from 20140601152943767 to 20140601 15:29:43.767.
Note #1: SELECT STUFF('abcef', 4, 1, 'DDD') will replace substring starting from index 4 with a length of 1 char (e) with DDD -> abcDDDf
Note #2: SELECT STUFF('abcef', 4, 0, 'DDD') returns abcDDDef
You can try lik ethis:
select
concat(convert(date,LEFT('20140601152943767',8)), ' ' , Convert(time,Dateadd(SECOND,
Right('20140601152943767',2)/1,
Dateadd(MINUTE,
Right('20140601152943767',4)/100,
Dateadd(hour,
Right('20140601152943767',6)/10000,
'1900-01-01')))) )
as myDate
Output:
2014-06-01 22:38:07.0000000
Well, first of all - you need conversion not to date but to datetime type.
Second - you should always specify format as mentioned:
https://msdn.microsoft.com/en-us/library/ms187928(v=sql.120).aspx
e.g.
select convert(datetime, '20140501', 112)
3 - there is no such format supported for your value demonstrated, so you have to modify your value yo something like yyyy-mm-ddThh:mi:ss.mmm (iso) or to make custom conversion with substring and so on.
;WITH myvalues AS (
SELECT '20140601152943767' value
)
SELECT
convert(date, LEFT(mv.value, 8), 112),
cast(STUFF(STUFF(STUFF(STUFF(mv.[value], 1, 8, ''), 7, 0, '.'), 5, 0, ':'), 3, 0, ':') AS TIME)
FROM myvalues mv
Try This one
declare #datetime varchar(20) = '20140601152943767'
select convert(varchar(20),convert(date,LEFT(#datetime,8))) + ' ' + substring(RIGHT(#datetime,9), 1, 2)
+ ':' + substring(RIGHT(#datetime,9), 3, 2)
+ ':' + substring(RIGHT(#datetime,9), 5, 2)
+ '.' + substring(RIGHT(#datetime,9), 7, 3)
I know some might argue that this is not the best way , but still it's an alternative.Few people have already used convert and stuff functions .
Try this out.Easy to understand.Also test your subqueries.
select y1||'-'||M1||'-'||d1||' '||h1||':'||mi||':'||s1||':'||f1 as xdate
from
(
select substr('20140601152943767',1,4) Y1 from dual
),
(
select substr('20140601152943767',5,2)M1 from dual
),
(
select substr('20140601152943767',7,2) d1 from dual
),
(
select substr('20140601152943767',9,2) h1 from dual
),
(
select substr('20140601152943767',11,2) mi from dual
),
(
select substr('20140601152943767',13,2) s1 from dual
),
(
select substr('20140601152943767',15,3) f1 from dual
)
Output:
2014-06-01 15:29:43.767

SQL Server : format 700 as time

I've run an import which has updated many records in my tblRota.StartTime and tblRota.EndTime in the format 900 and 1700.
How can I reformat these to 09:00 and 17:00?
The datatypes of both columns is varchar.
Thank you.
You could use this query:
select stuff(right('0' + replace([StartTime], ':', ''), 4), 3, 0, ':'),
stuff(right('0' + replace([EndTime], ':', ''), 4), 3, 0, ':')
from [tblRota]
The steps are:
Remove the :: replace([StartTime], ':', '')
Get the time on 4 digits: right('0' + <3Or4DigitTime>, 4)
Insert the :: stuff(<4DigitTime>, 3, 0, ':')
Use some string manipulation:
UPDATE tblRota
SET StartTime = LEFT(RIGHT('0'+StartTime , 4),2)+':'+RIGHT(StartTime ,2),
EndTime = LEFT(RIGHT('0'+EndTime , 4),2)+':'+RIGHT(EndTime ,2)