Solved

DB2: Casting and substring issues

Posted on 2011-09-08
3
1,759 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:
Kdo earned 500 total points
Comment Utility
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
Comment Utility

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:Kdo
Comment Utility
Hi ditallop,

The DIGITS function will work nicely, too.  :)


Kent
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now