问题描述:

I have a table "foo" with a varchar field "text" where some rows are prefixed with a "-" and others are not. What's the best way to update rows so that the "text" field begins with a "-" sign?

网友答案:
UPDATE foo SET text = concat('-', text) WHERE substring(text, 1, 1) <> '-'
网友答案:

This should do the trick:

UPDATE Foo
SET textField = CASE WHEN LEFT(textField,1) = '-' THEN textField 
                     ELSE CONCAT('-', textField) END
网友答案:
UPDATE `foo`
  SET `text` = CONCAT('-', text)
  WHERE SUBSTR(text, 1, 1) <> '-';
网友答案:
UPDATE foo
SET `text` = CONCAT('-', `text`)
WHERE `text` NOT LIKE '-%'
相关阅读:
Top