Solved

DB2: Casting and substring issues

Posted on 2011-09-08
3
1,812 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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…

830 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