Skip to content

Instantly share code, notes, and snippets.

@mhkeller
Last active March 15, 2016 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhkeller/c9084cbe1febefb92de1 to your computer and use it in GitHub Desktop.
Save mhkeller/c9084cbe1febefb92de1 to your computer and use it in GitHub Desktop.
Find replace in Postgresql
name code
Alabama 01
Alaska 02
// Put this formula in column C2 and drag down to prepend the letter `a` in front of the contents of column B2
// Excel requires double quotes
=CONCATENATE("a", B2)
-- Docs http://www.postgresql.org/docs/current/static/functions-string.html
-- Postgresql requires single quotes.
-- Generic template
UPDATE your-table-name SET your-column-name = REPLACE(your-column-name, 'string-to-replace', 'what-to-replace-it-with');
-- Example. Your table is called `my_table`, column is called `fips` and you've prepended an `a` before each value to maintain its identity as a string and now you want to remove it
UPDATE my_table SET fips = REPLACE(fips, 'a', '');
-- Better example. Add a new column called `clean_fips` so you can compare the results against the old. You can create a new column via the CartoDB interface.
UPDATE my_table SET clean_fips = REPLACE(fips, 'a', '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment