Reputation: 495
I have a table column with a bunch of text in each field. There is a specific sentence within each field that remains consistent (except for the actual price) that states:
"The price is $15,350."
I would like to parse out the dollar figure from each field and put it into its own column.
What is the SQL statement to make this happen?
Upvotes: 1
Views: 293
Reputation: 81479
Try this: (also check out the SQL Fiddle of the solution)
SELECT
REPLACE(
SUBSTRING(
SUBSTRING( myval,
LOCATE('The price is $', myval) + 15,
LENGTH(myval) - LOCATE('The price is $', myval)
), 1,
LOCATE('.',
SUBSTRING( myval,
LOCATE('The price is $', myval) + 15,
LENGTH(myval) - LOCATE('The price is $', myval))
)
), ',', '') as Result
FROM tbl
ORIGINAL ANSWER (kept since it covers some basic parsing concepts and also informs the last-submitted solution):
If things are that consistent then a simple series of REPLACE
functions to remove text around the original value with an empty strings should do it. Example:
SELECT your_column AS original_value,
REPLACE(REPLACE(your_column, 'The price is $', ''), '$', '') AS parsed_value
FROM your_table
OUTPUT:
original_value parsed_value The price is $15,350. 15,350.
You could wrap an additional REPLACE call to remove the comma ',' like this:
SELECT your_column AS original_value,
REPLACE(
REPLACE(
REPLACE(your_column,
'The price is $', ''), '$', ''), ',', '') AS parsed_value
FROM your_table
NEW OUTPUT:
original_value parsed_value The price is $15,350. 15350.
EDIT - Addressing additional text around "The price is..." block:
SELECT your_column AS original_value,
SUBSTRING('The price is $',
LOCATE ('The price is $', your_column)
REPLACE(
REPLACE(
REPLACE(your_column,
'The price is $', ''), '$', ''), ',', '')
,
AS parsed_value FROM your_table
The resulting value, 15350., should now easily cast to a decimal with an additional CAST(xxxxx AS DECIMAL(10,2))
wrapped around the result.
Upvotes: 1
Reputation: 661
mysql> select replace(replace(mid("The price is $15,350.", 15), ",", ""), ".", "");
+----------------------------------------------------------------------+
| replace(replace(mid("The price is $15,350.", 15), ",", ""), ".", "") |
+----------------------------------------------------------------------+
| 15350 |
+----------------------------------------------------------------------+
Upvotes: 1