data type conversion - Set numeric column to equal formatted varchar currency column in PostgreSQL -


i have varchar(1000) column of prices dollar signs (e.g. $100) , have created new numeric(15,2) column, i'd set equal prices in varchar column.

this worked me in mysql:

update  product_table set cost = replace(replace(price, '$', ''), ',',''); 

but in postgresql throws error:

error:  column "cost" of type numeric expression of type character line 2: set cost = replace(replace(price, '$', ''), ',','');                    ^ hint:  need rewrite or cast expression. 

i tried follow hint , tried google searches examples, small brain hasn't been able figure out.

in postgresql can in 1 swoop, rather replacing '$' , ',' s in separate calls:

update product_table set cost = regexp_replace(price, '[$,]', '', 'g')::numeric(15,2); 

in regexp_replace pattern [$,] means replace either of '$' or ',' replace string (the empty string '' in case), , 'g' flag indicates such patterns need replaced.

then need cast resulting string numeric(15,2) value.


Comments