标准的方式来表示(如文本)在SQL中的数字是一样的东西:
16000.00
15000.00
16000.00
所以,你逗号在文中伤害你。
解决这一问题将是存储数据只是作为一个numeric
,而不是使用字符串(text
,varchar
,character
)型的,如已被a_horse_with_no_name提出的最明智的方式。
然而,假设这是一个很好的理由做了,比如你继承了设计,你无法改变的,一种可能性是摆脱所有这一切都不是(减号,数字周期)之前的字符铸造数字:
让我们假设这是你的输入数据
CREATE TABLE tableone
(
bonus text
) ;
INSERT INTO tableone(bonus)
VALUES
('16,000.00'),
('15,000.00'),
('16,000.00'),
('something strange 25'),
('why do you actually use a "text" column if you could just define it as numeric(15,0)?'),
(NULL) ;
您可以删除所有与regexp_replace
和适当的正则表达式([^-0-9.]
)的straneous字符,并做到这一点g
lobally:
SELECT
CAST(
COALESCE(
NULLIF(
regexp_replace(bonus, '[^-0-9.]+', '', 'g'),
''),
'0')
AS numeric)
FROM
tableone ;
| coalesce |
| -------: |
| 16000.00 |
| 15000.00 |
| 16000.00 |
| 25 |
| 150 |
| 0 |
看看会发生什么的15,0(这可能不是你想要的)。
检查一切在dbfiddle here
究竟为什么你存储号码在'varchar'列? –