如何从描述中提取数字(价格)

How to extract a number (price) from description

商品价格格式

999,99

999 -  1 ..4 digits 
, - comma sign marks decimal point 
99 - 2 digits after price

Postgres 9.1 table 包含 char(50) 类型的列。 此列可能包含以空格分隔的单独单词中的价格。喜欢

Product 12,99 blue
Another product 4,59
This is third 124,39 item price

如何从 select 语句中的描述中提取价格?对于这 3 行,结果应该是

12,99
4,59
124,39

正在使用

x86_64-unknown-linux-gnu 上的 PostgreSQL 9.1.2,由 gcc-4 编译。4.real (Debian 4.4.5-8) 4.4.5,64 位

您可以使用 substring(yourString FROM yourPattern) 函数执行此操作。

SELECT substring(columnName FROM '\d{1,4},\d{2}') 
FROM tableName

正则表达式解释:

d{1,4} - 至少一位,但不超过四位

, - 逗号

d{2} - 刚好两位数