Link to home
Start Free TrialLog in
Avatar of Jeff
JeffFlag for United States of America

asked on

iSeries DB2 String to Time conversion issue

I have a field (LPSDT) that is a date and time in a string format: 20170312112507

In my query I substring the time as substr(LPSDT, 9, 2)||':'||substr(LPSDT, 11, 2)||':'||substr(LPSDT, 13, 2) which results in 11:25:07 but it is a string not an actual time format.

If I use the following: time(substr(lpsdt, 9, 6) I get 3/1/2017 11:25:07 AM as a result which makes no sense since I am substringing the data that only equates to the time and the date it is returning is wrong. The date does not match what is actually in the field. The system seems to be making assumptions about the data rather than simply returning the expected value.

I've tried to_date with a format of 'hh24miss' and get the same result.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeff

ASKER

To get it to work without error I use the following: to_date(char(lpsdt), 'YYYYMMDDHH24MISS')

The result is 3/10/2017 1:42:07 PM

It's not exactly what I was going for but I believe will work equally as well.
If you want to extract just the time, this worked for me on a V7R1 system:

select time(timestamp('20170312112507')) from sysibm/sysdummy1

Time() function can extract a time from a timestamp, or it can convert a properly formatted character variable.  Your substring isn't properly formatted - needs delimiters appropriate for the current date format in your SQL session.  Somehow, you're getting a timestamp returned, and since you're not supplying a date, system for some reason is substituting the first date of the current month.  Not sure why, but I made it happen when I did this:

select to_date('112507','HH24MISS') from sysibm/sysdummy1

Returned a timestamp with the date portion set to the cfirst day of the current month.

If you are running on IBM i DB2 V6R1 or later, you can use TIMESTAMP_FORMAT() or its alias TO_DATE() as Kent suggests, with one extra step to extract just the time portion of the returned timestamp:

select time(to_date('20170312112507','YYYYMMDDHH24MISS')) from sysibm/sysdummy1
What is the data type of LPSDT?  You said it was a string, so CHAR() conversion shouldn't do anything.  Is it numeric?
Avatar of Jeff

ASKER

Gary.

Running v7r3 on Power 8 with IASPHA

I was trying to extract the time and date separately out of a field but TIME() doesn't work as expected. Instead of getting 1:42 PM I get 11/30/1899 1:42:07 PM. Makes no sense to me.

Using the code I posted I get a result I can work with but I need to subtract lpfdt from lpsdt (finish time - start time) to get time spent which works fine but the result is 1927 instead of 19:27.
Avatar of Jeff

ASKER

Sorry. I jumped to the conclusion that it was a string. Turns out that it is stored as Packed Decimal. The database is a proprietary WMS. I'm doing reporting from it.
Avatar of Jeff

ASKER

Sorry. I jumped to the conclusion that it was a string. Turns out that it is stored as Packed Decimal. The database is a proprietary WMS. I'm doing reporting from it.
That makes more sense.  That's why you have to cast to char, then convert.  If you want the difference, you'll want to use the full timestamp, not just the time portion, since I assume start and end could span midnight?

Could you just show your code?  Saves me guessing.