»
S
I
D
E
B
A
R
«
Php Strip Dollar Sign
November 11th, 2009 by admin

php strip dollar sign
PHP/ MYSQL: How do I get rid of the Dollar Sign and the comma!?

Okay. I asked this last night at like 2:00 am, figuring that’s when the code-monkeys come out, but the monkeys were fairly quiet, so..

I have a table called lets say “price_table”
in that are these colums:
1) serial_number [INT(20)] (Key ID- non sequential but all unique)
2) vendor_price [VARCHAR(20)]
3) my_price [DECIMAL (10,2]

Now,… Vendor price looks like this: $1,234,56 with the dollar sign and the comma.Sooo Igotsta run some code that pulls the vendor_price as varchar, strips the dollar sign and the comma and sticks in back in to the database under my_price on the correct row as a decimal value…

I hope that makes sense..

You should update your table so that `vendor_price` is a numeric value instead of varchar. Otherwise there’s always possible issues when you need to compare the price, sort the output, etc.

UPDATE price_table SET vendor_price = REPLACE(vendor_price, ‘$’, ”);
UPDATE price_table SET vendor_price = REPLACE(vendor_price, ‘,’, ”);

Then change the data type from varchar to one of the numeric data types.

If you don’t want to do that for whatever reason, you can still use the ‘REPLACE()’ function in your select queries as such:

SELECT serial_number, REPLACE( REPLACE(vendor_price, ‘$’, ”), ‘,’, ” ) AS vendor_price, my_price FROM price_table;


Leave a Reply

»  Substance: WordPress   »  Style: Ahren Ahimsa