I have a table that sometimes gets updated with records that miss a ‘0’ or several consecutive zeroes. The records length should be 8 digits beyond the decimal point (10 characters in total).
For example – a record that originally looks like 1.12345600 will end up like 1.123456
I made a script that checks all the records length and adds to each record that is shorter than 10 characters a ‘0’.
The problem is that it only add a ‘0’ once. The above example would look like 1.1234560 and not 1.12345600 as I want it.
This is the script :
DECLARE
CURSOR dif IS
SELECT CUST_CODE, CUST_ID, CONTRACT_NUM, MSISDN
FROM project1;
BEGIN
FOR a in dif LOOP
IF LENGTH (a.CUST_CODE)<10
THEN
UPDATE project1
SET CUST_CODE=a.CUST_CODE||'0'
WHERE CUST_CODE=a.CUST_CODE;
END IF;
END LOOP;
commit;
END;
After it finishes running, a single ‘0’ is added. If I’ll run the script again, it will add another ‘0’ to any records that is still shorter than 10 characters. Reocrds that have 7 characters would require running it a 3rd time.
I’m guessing there should be another loop somewhere to keep checking the records until it all reaches the required length.
Any idea?
You can use rpad function: http://www.techonthenet.com/oracle/functions/rpad.php (the third example in the link is what you want).