Re: Doing a regexp-based search/replace?
От | Josh Berkus |
---|---|
Тема | Re: Doing a regexp-based search/replace? |
Дата | |
Msg-id | web-150709@davinci.ethosmedia.com обсуждение исходный текст |
Ответ на | Doing a regexp-based search/replace? (Steve Frampton <frampton@LinuxNinja.com>) |
Список | pgsql-sql |
Steve, > I've got a table containing property_id's with values of the form > ###-####. I would like to discard the slash onwards (and I can't use > a > substr() because I am not guaranteed if a) the -#### portion exists, > b) > what position it exists from. > > If this were a text file, I would use a sed expression such as: > > cat textfile | sed 's/-.*$//' In SQL/plpgsql, you can't do this with a single expression. However, you can do it with three expressions put together. CREATE FUNCTION remove_propid_tail (VARCHAR ) RETURNS VARCHAR AS' SELECT SUBSTR($1, 1, ((STRPOS($1, ''-'') - 1)); END;' LANGUAGE 'SQL'; Then run: UPDATE main_table SET property_id = remove_propid_tail(property_id) WHERE property_id ~ '-'; -Josh ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete informationtechnology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco
В списке pgsql-sql по дате отправления: