1

I have following query and it is updating MAC address which are missing hyphnes but for the other who alerady have them it is changing them to null. Try to update values in one column.

UPDATE mytable SET MAC =
CASE
 WHEN MAC NOT LIKE '%-%' 
  THEN UPPER(CONCAT(SUBSTR(MAC,1,2),'-', SUBSTR(MAC,3,2),'-', SUBSTR(MAC,5,2),'-', SUBSTR(MAC,7,2),'-', SUBSTR(MAC,9,2),'-', SUBSTR(MAC,11,2)))
 END;

Any suggestions woild be great.

Thanks

2 Answers 2

3

Just add an ELSE case which sets the value to its current value:

UPDATE mytable SET MAC =
CASE
 WHEN MAC NOT LIKE '%-%' 
  THEN UPPER(CONCAT(SUBSTR(MAC,1,2),'-', SUBSTR(MAC,3,2),'-', SUBSTR(MAC,5,2),'-', SUBSTR(MAC,7,2),'-', SUBSTR(MAC,9,2),'-', SUBSTR(MAC,11,2)))
  /* Else case sets it to its current value to avoid NULL */
  ELSE MAC
 END;

It's also possible to use a WHERE clause so rows that should not be updated are not matched in the first place. That is probably the more appropriate action to take in this instance, unless you have other conditions to apply in the CASE

UPDATE mytable 
SET MAC = UPPER(CONCAT(SUBSTR(MAC,1,2),'-', SUBSTR(MAC,3,2),'-', SUBSTR(MAC,5,2),'-', SUBSTR(MAC,7,2),'-', SUBSTR(MAC,9,2),'-', SUBSTR(MAC,11,2)))
WHERE MAC NOT LIKE '%-%'
Sign up to request clarification or add additional context in comments.

Comments

1
UPDATE mytable 

SET MAC = UPPER(CONCAT(SUBSTR(MAC,1,2),'-', SUBSTR(MAC,3,2),'-', SUBSTR(MAC,5,2),'-', SUBSTR(MAC,7,2),'-', SUBSTR(MAC,9,2),'-', SUBSTR(MAC,11,2)))

WHERE  MAC NOT LIKE '%-%' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.