Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DB2: Casting and substring issues

Posted on 2011-09-08
3
Medium Priority
?
1,903 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:Paula DiTallo
[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 46

Accepted Solution

by:
Kent Olsen earned 2000 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:Paula DiTallo
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 46

Expert Comment

by:Kent Olsen
ID: 36505459
Hi ditallop,

The DIGITS function will work nicely, too.  :)


Kent
0

Featured Post

Google Certified Professional - Cloud Architect

This course (1 of 3) is designed to help students who are interested in Google Cloud Platform (GCP) to become familiar with the platform, navigate the console and learn its capabilities. It will also prepare students for the Google Cloud Architect certification exam.

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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

688 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