Avatar of Jeff
Jeff
Flag for United States of America asked on

DB2 SQL Convert Packed Decimal to Time with Formatting

LPSDT and LPFDT are date and time stored as Packed Decimal. I am attempting to extract the information out and put it in time format and calculate Time Spent.

select LPEM as "Employee", LPLC as "LaborCode", 
to_date(char(lpsdt), 'YYYYMMDDHH24MISS') as "Start Date/Time",
to_date(char(lpfdt), 'YYYYMMDDHH24MISS') as "Finish Date/Time",
to_date(char(lpfdt), 'YYYYMMDDHH24MISS') - to_date(char(lpsdt), 'YYYYMMDDHH24MISS') as "Time Spent",
from labor.empmngmnt

Open in new window

SQLDB2* AS400

Avatar of undefined
Last Comment
Kent Olsen

8/22/2022 - Mon
Kent Olsen

Hi Jeff,

This has the potential to solve just like your previous question.  The packed decimal value is 14 digits.  Convert them to timestamps, and compute the difference with timestampdiff.  It does require a reasonable estimate of what you expect, seconds, days, etc.

select LPEM as "Employee", LPLC as "LaborCode", 
to_date(char(lpsdt), 'YYYYMMDDHH24MISS') as "Start Date/Time",
to_date(char(lpfdt), 'YYYYMMDDHH24MISS') as "Finish Date/Time",
timestampdiff (2, to_date(char(lpfdt), 'YYYYMMDDHH24MISS'), to_date(char(lpsdt), 'YYYYMMDDHH24MISS')) as "Time Spent",
from labor.empmngmnt

Open in new window


That query will return the number of seconds between the two times.  Adding the computed number of seconds to time ('00.00.00') will give you an answer as HH:MM:SS.  If you're expecting an answer of days or more, what format do you need?

Kent
Jeff

ASKER
I get an error Number of arguments for function TimeStampDiff not valid...
Kent Olsen

Sorry.  There are too many kinds of DB2.  :)  Try this one....

select LPEM as "Employee", LPLC as "LaborCode", 
to_date(char(lpsdt), 'YYYYMMDDHH24MISS') as "Start Date/Time",
to_date(char(lpfdt), 'YYYYMMDDHH24MISS') as "Finish Date/Time",
timestampdiff (2, cast (to_date(char(lpfdt), 'YYYYMMDDHH24MISS') -  to_date(char(lpsdt), 'YYYYMMDDHH24MISS') as char(22))) as "Time Spent",
from labor.empmngmnt

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Jeff

ASKER
Sorry for the absence.

While the code produces the result it either isn't correct or represents a value that I'm not expecting.

For  a Start of 3/19/2017 1:44:23 PM and a Finish of 3/19/2017 2:06:21 PM

timestampdiff (2, cast (to_date(char(lpfdt), 'YYYYMMDDHH24MISS') -  to_date(char(lpsdt), 'YYYYMMDDHH24MISS') as char(22))) as "Time Spent",

results in 13:18

For the same Start and Finish

to_date(char(lpfdt), 'YYYYMMDDHH24MISS') - to_date(char(lpsdt), 'YYYYMMDDHH24MISS') as "Time Spent",

results in 21:58

21:58 is the result that I expect.

Also with both the result comes out as an integer value instead of time. They are 1318 and 2158 instead of 13:18 and 21:58.
ASKER CERTIFIED SOLUTION
Kent Olsen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.