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

LVL 1
JeffDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
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
0
JeffDeveloperAuthor Commented:
I get an error Number of arguments for function TimeStampDiff not valid...
0
Kent OlsenDBACommented:
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

0
JeffDeveloperAuthor Commented:
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.
0
Kent OlsenDBACommented:
It's interesting that the first one comes up with a different value that the second.  I'll have to look into that.

So let's take an easier path.  You can add a decimal(6,0) to a time value and get the expected results.  Since (lpfdt-lpsdt) gives a value of 2158.000000, the solution would see to be:

  SELECT time('00:00:00') + cast((lpfdt-lpsdt) as dec(6,0)) as "Time Spent"

That should work fine and should be universal within the DB2 family.  The path I'd originally taken seems specific to certain flavors/releases of DB2.

Kent
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.