SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Adding YYYYMMDD and HHMMSS to get mm/dd/yyyy HH:MM:SS


Adding YYYYMMDD and HHMMSS to get mm/dd/yyyy HH:MM:SS

Author
Message
SQLCurious
SQLCurious
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 450
I am writing some reporting queries to show execution start and end times of my SQL jobs. I need to show execution start and end times for each of these jobs in "MM/DD/YYYY HH:MM:SS" format.

SysJobHistory has data in integer format (YYYYMMDD for date; HHMMSS for time; execution time as integer respectively in run_date, run_time, run_duration columns).
What is the efficient way to do this. For now, I am doing it in a very crude way as below:


select sjh.Job_ID, CONVERT(varchar(20), CONVERT(date, CONVERT(varchar(8), sjh.run_date), 112),110) run_date,
CONVERT(date, CONVERT(varchar(8), sjh.run_date), 112) run_date2,
DATEADD ( -- Add execution seconds to the start time after converting YYYYMMDD HHMMSS to MM/DD/YYYY HH:MM:SS format
ss,
sjh.Run_Duration,
CAST (
SUBSTRING (CAST (run_date as VARCHAR), 5, 2) + '/' + -- MM
SUBSTRING (CAST (run_date as VARCHAR), 7, 2) + '/' + -- DD
SUBSTRING (CAST (run_date as VARCHAR), 1, 4) + ' ' + -- YYYY
SUBSTRING (REPLICATE ('0', 6-LEN(CAST (Run_Time AS VARCHAR))) + CAST (Run_Time AS VARCHAR), 1, 2) + ':' + -- HH (add a 0 at the beginning to account for single digit hours)
SUBSTRING (CAST (Run_Time AS VARCHAR), 3, 2) + ':' + -- MM
SUBSTRING (CAST (Run_Time AS VARCHAR), 5, 2) -- SS
AS DATETIME)
) AS NewRunTime,
sjh.run_time, sjh.run_duration, sjh.run_status
from msdb..sysjobhistory sjh -- ON j.ExternalJobID = sjh.Job_id
where sjh.run_date = CONVERT (VARCHAR (8), GetDate(), 112) and sjh.step_id = 0 and run_status in (0, 1)



Your inputs will be appreciated.

SQLCurious
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38987 Visits: 38508
How about this (sorry, but I didn't work with your query):



select
*,
cast(cast(run_date as varchar) + ' ' + stuff(stuff(right('000000' + cast(run_time as varchar),6),5,0,':'),3,0,':') as datetime)
from
msdb.dbo.sysjobhistory;




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16272 Visits: 19059
There's no need to make that extra work with the date. You need to work with the run_duration column because it comes in format HHMMSS as well.

select sjh.Job_ID,
run_date,
CONVERT(varchar(20), CONVERT(date, CONVERT(varchar(8), sjh.run_date), 112),110) run_date,
CONVERT(date, CONVERT(varchar(8), sjh.run_date), 112) run_date2,
DATEADD ( -- Add execution seconds to the start time after converting YYYYMMDD HHMMSS to MM/DD/YYYY HH:MM:SS format
ss,
((sjh.Run_Duration / 100) * 60) + (sjh.Run_Duration % 100),
CAST( run_date AS char(8)) + ' '
+ STUFF( STUFF( RIGHT( '00000' + CAST( sjh.run_time as varchar(6)), 6), 3, 0, ':'), 6, 0, ':')
) AS NewRunTime,
sjh.run_duration,
sjh.run_status
from msdb..sysjobhistory sjh -- ON j.ExternalJobID = sjh.Job_id
where sjh.run_date = CONVERT (VARCHAR (8), GetDate(), 112)
and sjh.step_id = 0
and run_status in (0, 1)


Note that I don't expect jobs that run for one hour or more.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
SQLCurious
SQLCurious
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 450
Thank you guys! I appreciate that.

SC
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)SSC-Dedicated (38K reputation)

Group: General Forum Members
Points: 38987 Visits: 38508
Modified the dateadd just in case you have jobs that run an hour or more:



select
sjh.job_id,
CONVERT(varchar(20), CAST(CAST(sjh.run_date as char(8)) as date),110) run_date,
CAST(CAST(sjh.run_date as char(8)) as date) run_date2,
DATEADD ( -- Add execution seconds to the start time after converting YYYYMMDD HHMMSS to MM/DD/YYYY HH:MM:SS format
ss,
((((sjh.run_duration / 10000) * 60) + (sjh.run_duration / 100) % 100) * 60) + (sjh.run_duration % 100),
CAST(CAST(sjh.run_date as char(8)) + ' ' + STUFF(STUFF(right('000000' + CAST(sjh.run_time as char(6)),6),5,0,':'),3,0,':') as datetime)
) NewRunTime,
sjh.run_time,
sjh.run_duration,
sjh.run_status
from
msdb..sysjobhistory sjh -- ON j.ExternalJobID = sjh.Job_id
where
sjh.run_date = CONVERT(VARCHAR(8), GetDate(), 112) and
sjh.step_id = 0 and
run_status in (0, 1)




I had to look closer at Luis' code as the outer stuff confused me at first until I realized he was stuffing them in left to right and I did it right to left.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Eirikur Eiriksson
Eirikur Eiriksson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14888 Visits: 18591
Didn't see Lynn's code before I wrote the example, almost the same but without a string thingy
Cool


/*Converting INT date and time to datetime */
DECLARE @INT_YYYYMMDD INT = 20140704;
DECLARE @INT_HHMMSS INT = 012056;
SELECT DATEADD(SECOND,
(((@INT_HHMMSS / 10000) * 3600) -- hours to seconds
+ (((@INT_HHMMSS / 100) % 100) * 60) -- minutes to seconds
+ (@INT_HHMMSS % 100)) -- seconds
, CONVERT(DATETIME2(0),CAST(@INT_YYYYMMDD AS VARCHAR(8)),112))



Results
2014-07-04 01:20:56

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84865 Visits: 41069
Eirikur Eiriksson (6/15/2014)
Didn't see Lynn's code before I wrote the example, almost the same but without a string thingy
Cool


/*Converting INT date and time to datetime */
DECLARE @INT_YYYYMMDD INT = 20140704;
DECLARE @INT_HHMMSS INT = 012056;
SELECT DATEADD(SECOND,
(((@INT_HHMMSS / 10000) * 3600) -- hours to seconds
+ (((@INT_HHMMSS / 100) % 100) * 60) -- minutes to seconds
+ (@INT_HHMMSS % 100)) -- seconds
, CONVERT(DATETIME2(0),CAST(@INT_YYYYMMDD AS VARCHAR(8)),112))



Results
2014-07-04 01:20:56


Just curious... since the use of CONVERT kills the possibility of portability anyway (and I don't believe in portable code for anything but C.R.U.D.), why did you feel it necessary to use DATETIME2? Definitely not a challenge here... just curious.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Eirikur Eiriksson
Eirikur Eiriksson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14888 Visits: 18591
Jeff Moden (6/15/2014)
Eirikur Eiriksson (6/15/2014)
Didn't see Lynn's code before I wrote the example, almost the same but without a string thingy
Cool


/*Converting INT date and time to datetime */
DECLARE @INT_YYYYMMDD INT = 20140704;
DECLARE @INT_HHMMSS INT = 012056;
SELECT DATEADD(SECOND,
(((@INT_HHMMSS / 10000) * 3600) -- hours to seconds
+ (((@INT_HHMMSS / 100) % 100) * 60) -- minutes to seconds
+ (@INT_HHMMSS % 100)) -- seconds
, CONVERT(DATETIME2(0),CAST(@INT_YYYYMMDD AS VARCHAR(8)),112))



Results
2014-07-04 01:20:56


Just curious... since the use of CONVERT kills the possibility of portability anyway (and I don't believe in portable code for anything but C.R.U.D.), why did you feel it necessary to use DATETIME2? Definitely not a challenge here... just curious.


Just being slightly stingy here;-) saving two bytes
Cool

DECLARE @DT DATETIME      = GETDATE();
DECLARE @DT2 DATETIME2(0) = GETDATE();

SELECT 'DATETIME' AS DATA_TYPE, DATALENGTH(@DT) AS DATA_LENGTH
UNION ALL
SELECT 'DATETIME2(0)', DATALENGTH(@DT2);



Results
DATA_TYPE    DATA_LENGTH
------------ -----------
DATETIME 8
DATETIME2(0) 6


And of course I have been careful not to do funny stuff with datetime since someone told me off a while back :-D
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84865 Visits: 41069
Eirikur Eiriksson (6/15/2014)

And of course I have been careful not to do funny stuff with datetime since someone told me off a while back :-D


Gosh, I hope that wasn't me. I prefer DATETIME over the other datatypes because you can subtact a start date from and end date to get the duration in one easy step (sans final formatting).

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Eirikur Eiriksson
Eirikur Eiriksson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14888 Visits: 18591
Jeff Moden (6/15/2014)
Eirikur Eiriksson (6/15/2014)

And of course I have been careful not to do funny stuff with datetime since someone told me off a while back :-D


Gosh, I hope that wasn't me. I prefer DATETIME over the other datatypes because you can subtract a start date from and end date to get the duration in one easy step (sans final formatting).


Cannot remember who it was, the name sounded like Just Married, who ever that isHehe
Cool
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search