Solved

DB2: Casting and substring issues

Posted on 2011-09-08
3
1,840 Views
Last Modified: 2012-05-12
Hi Techies--

OK, it looks like there's an issue with leading "0's" with dates and months on a substring/cast conversion.  The date is sitting in the PRTED8 field as decimal(8,0) like 12311995 - or December 31, 1995.  That date will convert correctly to 12/31/1995 for "TermDate-Date" and 1995-12-31 for "TermDate-Varchar"  If I have a date like 01311996 what I get is: NULL for "TermDate-Date" and 996-13-11 for "TermDate-Varchar" . What is the best way to solve this issue.


SELECT
       CAST(SUBSTRING(CAST(PRTED8 AS VARCHAR( 8 )),5,4) || '-' || SUBSTRING(CAST(PRTED8 AS VARCHAR( 8 )),1,2) || '-' || SUBSTRING(CAST(PRTED8 AS VARCHAR( 8 )),3,2) AS VARCHAR(10)) AS "TermDate-Varchar",
       CAST(SUBSTRING(CAST(PRTED8 AS VARCHAR( 8 )),5,4) || '-' || SUBSTRING(CAST(PRTED8 AS VARCHAR( 8 )),1,2) || '-' || SUBSTRING(CAST(PRTED8 AS VARCHAR( 8 )),3,2) AS DATE) AS "TermDate-Date",
       DIGITS(PRTED8) AS "TermDate-Decimal"
 FROM XXXTST.SSSSS
0
Comment
Question by:ditallop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 36505029
Hi ditallop,

My guess is that the issue is that there is NO leading zero when the day is less than 10.  The conversion from decimal drops the leading zero.

But that's easy enough to solve.  :)

 SELECT right ('0' + cast (decimal_value as varchar(8)), 8) ...

That will ensure that you're converting an 8 character string.


Of course, you can do this with integer math, too.

SELECT
  decimal_value / 1000000 as Day,
  mod (decimal_value / 1000, 100) as month,
  mod (decimal_value, 10000) as year
FROM ....


Good Luck,
Kent
0
 

Author Comment

by:ditallop
ID: 36505310

Very nice-- Thank you. The only thing I would add, is that I had the idea to issue digits(decimal_value) before the casting as an alternative to '0'  + cast(decimal_value

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 36505459
Hi ditallop,

The DIGITS function will work nicely, too.  :)


Kent
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question