Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update whole Column values in a table in oracle sql to a different format where different

Consider Table barcode has a column named b_values has values stored in two different formats. One with dashes and other without.

e.g., XXXXXXXXXXXXXXX -> XXX-XXXX-XXXXXXX-X

Length without dashes is 15 and with dashes 18.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried following but it did not work.

UPDATE barcode b
   SET b.barcode = CASE
                     WHEN b.barcode LIKE '%-%-%-%' THEN
                      b.barcode
                     WHEN b.barcode NOT LIKE '%-%-%-%' AND LENGTH(b.barcode) = 15 THEN
                      SUBSTR(b.barcode, 0, 3).CONCAT('-').CONCAT(SUBSTR(b.barcode, 4, 4)).CONCAT('-').
                      CONCAT(SUBSTR(b.barcode, 7, 7)).CONCAT('-').CONCAT(SUBSTR(b.barcode, 14, 1))
                     ELSE
                      b.barcode
                   END

I am trying to work on a script which will find all values without dashes in same column and update them including dashes.

I don’t need the values in a result from query. I’m trying to update the values stored in table.

>Solution :

You can do it as follows :

UPDATE mytable
SET barcode = SUBSTR(barcode, 1, 3) || '-' || SUBSTR(barcode, 4, 4) || '-' || SUBSTR(barcode, 8, 7) || '-' ||  SUBSTR(barcode, 15, 1)               
WHERE barcode NOT LIKE '%-%-%-%' AND LENGTH(barcode) = 15

Demo here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading