The strings (column values) begun with a character and rest of the string has numbers. We want the sum of these numbers −
J230 A130s C13
For this, use SUBSTRING() function along with SUM().
Let us first create a table −
mysql> create table DemoTable761 (Price varchar(100)); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable761 values('J230'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable761 values('A130'); Query OK, 1 row affected (0.70 sec) mysql> insert into DemoTable761 values('C13'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable761 values('D456'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable761 values('B6'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable761;
This will produce the following output -
+-------+ | Price | +-------+ | J230 | | A130 | | C13 | | D456 | | B6 | +-------+ 5 rows in set (0.00 sec)
Following is the query to find the sum by removing first character from a string followed by numbers in MySQL −
mysql> select sum(substring(Price,2)) from DemoTable761;
This will produce the following output -
+-------------------------+ | sum(substring(Price,2)) | +-------------------------+ | 835 | +-------------------------+ 1 row in set (0.00 sec)