We help IT Professionals succeed at work.

iSeries DB2 String to Time conversion issue

Jeff
Jeff asked
on
752 Views
Last Modified: 2018-02-15
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?
Comment
Watch Question

Data Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
JeffDeveloper

Author

Commented:
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.
Gary PattersonVP / Senior Consultant
CERTIFIED EXPERT

Commented:
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
Gary PattersonVP / Senior Consultant
CERTIFIED EXPERT

Commented:
What is the data type of LPSDT?  You said it was a string, so CHAR() conversion shouldn't do anything.  Is it numeric?
JeffDeveloper

Author

Commented:
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.
JeffDeveloper

Author

Commented:
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.
JeffDeveloper

Author

Commented:
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.
Gary PattersonVP / Senior Consultant
CERTIFIED EXPERT

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.