How to add $ and decimal to column in mysql

David Morin :

Im trying to add decimal and $ to all the rows in a table. Some of the rows have the $ and . in the correct spot. I searched on here but did not find a similar question when you have 2 different types of numbers.

The current format is like this:

Item 1    $10.00
Item 2    10
Item 3    99.99
Item 4    $120.00

I need to have it look like this.

Item 1    $10.00
Item 2    $10.00
Item 3    $99.99
Item 4    $120.00
nbk :

I would recommend using 2 differnet columns for corrency simbyl and the actual value, so that you can make summes and spo on with it easily.

but if you want this.

Use

UPDATE table1 SET `value` = CONCAT('$',TRUNCATE(`value`,2)) WHERE `value` NOT LIKE '$%'

TRUNCATE is used to give the numbers 2 digits

CREATE TABLE table1 (
  `name` VARCHAR(6),
  `value` VARCHAR(7)
);

INSERT INTO table1
  (`name`, `value`)
VALUES
  ('Item 1', '$10.00'),
  ('Item 2', '10'),
  ('Item 3', '99.99'),
  ('Item 4', '$120.00');
✓

✓
SELECT * FROM table1 WHERE `value` NOT LIKE '$%'
name   | value
:----- | :----
Item 2 | 10   
Item 3 | 99.99
UPDATE table1 SET `value` = CONCAT('$',TRUNCATE(`value`,2)) WHERE `value` NOT LIKE '$%'
SELECT * FROM table1
name   | value  
:----- | :------
Item 1 | $10.00 
Item 2 | $10.00 
Item 3 | $99.99 
Item 4 | $120.00

db<>fiddle here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=170721&siteId=1