To update all rows, but avoiding rows where either column is empty, you can use:
UPDATE info
SET
country = SUBSTRING_INDEX(country, "/", 1),
state = SUBSTRING_INDEX(country, "/", -1)
WHERE country <> "" AND state = ""
Did some slight dabbling, and it appears the better way to ensure that the existing data is protected is to only apply the change where state is empty and country is not empty. This prevents states already populated from being changed (even if that leaves a country with value of "United States/TX" when state has value of "TX" (or "CA" or "Bluegrass Home").
Also, my dabblings found that
SELECT SUBSTRING_INDEX("United States", "/", -1);
SELECT SUBSTRING_INDEX("United States", "/", 1);
will both return "United States" so either have some check that "/" exists (using index check of some kind) or have complicated logic that confirms that derived values for temp_country and temp_state are not equivalent.
I'm sure there is something more elegant and less error-prone. Still poking around for native functions. I think a set of domain rules would be good. Something like:
- country can contain "Country/State" OR "Country" OR ""
- state can contain "State" Or ""
- If state contains "" and country contains "Country/State", update country to just Country and Update state to State value in Country.
- If state contains "" and country contains "Country", leave both alone
- If state contains "State" and country contains "Country/State" > This is your predicament : Do you favor the "/State" value in country column, or "State" value in state column? If you favor leaving state as is, do you wipe out "/State" value in country column so that it's just "Country" or do you favor the value of "/State" in country column and wipe out existing "State" value in state column?
This will update the state where the state is currently empty and the country has Country/State:
UPDATE info
SET
state = IF(SUBSTRING_INDEX(country, "/", -1) = country, state, SUBSTRING_INDEX(country, "/", -1)),
country = IF(SUBSTRING_INDEX(country, "/", -1) = country, country, SUBSTRING_INDEX(country, "/", 1))
WHERE state = ""
It gets a bit more bold and froggy if you remove that state = "" condition, as it then forces you to add logic to determine if current value in state should hold or be overwritten by state from country field.