Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > IBM DB2 > Re: How to add ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 9 Topic 8855 of 9520
Post > Topic >>

Re: How to add leading zeroes

by Tonkuma <tonkuma@[EMAIL PROTECTED] > May 6, 2008 at 12:21 AM

> IMO, this kind of problem is better solved in the application layer
>
> /Lennart
Although I agree with Lennart, I want to try in SQL simply for my
entertainment.
------------------------------ Commands Entered
------------------------------
WITH Test_data(str) AS (
VALUES
 ('1.4.1')
,('1.10.1')
,('1.10.1.2.2')
,('1.22.99.1')
,('2')
,('2.8.11')
,('2.7.7')
)
,pos_dot AS (
SELECT str, index
     , INT(ROWNUMBER() OVER(PARTITION BY str
                                ORDER BY index) ) ordinal
  FROM Test_data
     , LATERAL(SELECT 1+n1+10*n2+100*n3
          FROM (VALUES 0,1,2,3,4,5,6,7,8,9) N(n1)
             , (VALUES 0,1,2,3,4,5,6,7,8,9) N(n2)
             , (VALUES 0,1,2,3,4,5,6,7,8,9) N(n3)
         WHERE n1+10*n2+100*n3 <= LENGTH(str)
       ) N(index)
 WHERE SUBSTR(str||'.',index,1) = '.'
)
,add_leading_zero
 (ordinal, index, str, result, str_order) AS (
SELECT 0, 0, str, CAST(str AS VARCHAR(50) )
     , ROWNUMBER() OVER()
  FROM Test_data
/* */ UNION ALL /* */
SELECT pre.ordinal + 1
     , new.index
     , pre.str
     , INSERT( pre.result
             , pre.ordinal*4 + 1
             , 0
             , SUBSTR('00', 1, 4 - new.index + pre.index) )
     , pre.str_order
  FROM add_leading_zero pre
     , pos_dot          new
 WHERE pre.ordinal < 4000
   AND new.str = pre.str
   AND new.ordinal = pre.ordinal + 1
)
SELECT str AS "source string"
     , result AS "3 digit numbers"
  FROM add_leading_zero T1
 WHERE ordinal
       = (SELECT MAX(ordinal)
            FROM pos_dot T2
           WHERE T2.str = T1.str
         )
 ORDER BY str_order
;
------------------------------------------------------------------------------

source string 3 digit numbers
------------- --------------------------------------------------
1.4.1         001.004.001
1.10.1        001.010.001
1.10.1.2.2    001.010.001.002.002
1.22.99.1     001.022.099.001
2             002
2.8.11        002.008.011
2.7.7         002.007.007

  7 record(s) selected.
 




 9 Posts in Topic:
How to add leading zeroes
"lenygold via DBMons  2008-05-05 15:27:03 
Re: How to add leading zeroes
Lennart <Erik.Lennart.  2008-05-05 12:14:06 
Re: How to add leading zeroes
Tonkuma <tonkuma@[EMAI  2008-05-06 00:21:47 
Re: How to add leading zeroes
--CELKO-- <jcelko212@[  2008-05-06 11:39:29 
Re: How to add leading zeroes
"Serman D." <  2008-05-06 00:24:48 
Re: How to add leading zeroes
"lenygold via DBMons  2008-05-06 11:48:54 
Re: How to add leading zeroes
Tonkuma <tonkuma@[EMAI  2008-05-06 05:59:15 
Re: How to add leading zeroes
"lenygold via DBMons  2008-05-07 15:38:05 
Re: How to add leading zeroes
Lennart <Erik.Lennart.  2008-05-06 08:13:10 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Dec 2 21:22:56 CST 2008.