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 4 of 9 Topic 8855 of 9520
Post > Topic >>

Re: How to add leading zeroes

by --CELKO-- <jcelko212@[EMAIL PROTECTED] > May 6, 2008 at 11:39 AM

>> Although I agree with Lennart, I want to try in SQL simply for my
entertainment. <<

I agree, but it is fun. Life is easier if the input string always ends
with a period, and I think that is required in one of the ISO or US
Government Standards for outlines.

1) Can you nest function calls 100 levels deep? So we just nest
REPLACE() calls that deep:

SELECT REPLACE ( ..
              REPLACE (sourcestring, '1.', '001.'),
			    ..
       '99.', '099.')
  FROM Foobar;

Since the functions will be on the stack, this ought to run pretty
fast.  But only a LISP programmer would love it.

2) Go back to procedural programming and write a loop?

CREATE TABLE LeadingZeros
(instring VARCHAR(3) NOT NULL PRIMARY KEY,
 outstring CHAR(4) NOT NULL
    CHECK (outstring SIMILAR TO '[:DIGITS:][:DIGITS:][:DIGITS:]\.');

INSERT INTO LeadingZeros (convert_nbr, instring, outstring)
VALUES (1, '1.', '001.'),  (2, '2.', '002.'), .., (99, '99.', '099.');

CREATE PROCEDURE PaddingZeros ()
LANGUAGE SQL
READS SQL DATA
BEGIN DECLARE i INTEGER;
SET i = 1;
WHILE i < 100
 DO UPDATE Foobar
       SET sourcestring
	       = REPLACE(sourcestring,
		             (SELECT instring
					    FROM LeadingZeros
				       WHERE i = convert_nbr),
		             (SELECT outstring
					    FROM LeadingZeros
				       WHERE i = convert_nbr));
	   SET i = i+1;
 END WHILE;
END;

This can be done with a recursive CTE, but that might be even worse.
 




 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 20:26:28 CST 2008.