This only gives me the last character (numeric), but I need an integer numeric string
SELECT substring('123 Main Street' FROM '%#"[0-9]#"%' FOR '#')
This gives me the same results, but I need to return an empty value:
SELECT substring('Main 123 Street' FROM '%#"[0-9]#"%' FOR '#')
NOTE: Postgres 7.4
Useful link: http://www.postgresql.org/docs/7.4/static/functions-matching.html
UPDATE:
SELECT substring('Main 123 Street' FROM '[0-9]+') SELECT substring('123 Main Street' FROM '[0-9]+')
- Both are now returning: 123
- Still need to skip or return the full line: "123 Main Street"
UPDATE 2:
Almost he has:
This gives me the results I want if it does not start with a numerical value:
SELECT COALESCE(substring('Main 123 Street' FROM '[0-9]*') || 'Main 123 Street', ''), substring('Main 123 Street' FROM '[0-9]*')
But this gives me both, and I only need the second condition:
SELECT COALESCE(substring('123 Main Street' FROM '[0-9]*') || '123 Main Street', ''), substring('123 Main Street' FROM '[0-9]*')
I GOT IT!!! Thanks to everyone who posted:
SELECT CASE WHEN COALESCE(substring(db_column FROM '[0-9]*'), db_column) != '' THEN COALESCE(substring(db_column FROM '[0-9]*'), db_column) ELSE db_column END AS addsress_string FROM db_table
source share