在postgresql中将百分比(字符串,带a%)转换为十进制
我想对postgresql中下表中每个人的分数(字符串)进行平均在postgresql中将百分比(字符串,带a%)转换为十进制,sql,postgresql,casting,Sql,Postgresql,Casting,我想对postgresql中下表中每个人的分数(字符串)进行平均 No. | Name | Term | Score 1 | A | 1 | 95.00% 2 | A | 2 | 99.00% 3 | C | 1 | 90.00% 4 | D | 1 | 100.00% . . 它不喜欢分数上的%。如何将其从包含%的字符串转换为十进制/浮点,如上图所示 试过了, score::de
No. | Name | Term | Score
1 | A | 1 | 95.00%
2 | A | 2 | 99.00%
3 | C | 1 | 90.00%
4 | D | 1 | 100.00%
.
.
它不喜欢分数上的%。如何将其从包含%的字符串转换为十进制/浮点,如上图所示
试过了,
score::decimal
但它抱怨说
ERROR: invalid input syntax for type numeric: "95.00%"
SQL state: 22P02
cast
似乎也不起作用
如何转换此值?一种方法使用
replace()
:
如果您确实想将其转换为介于0和1之间的数字,而不是0和100之间的数字,请尝试使用案例
:
select (case when right(score, 1) = '%'
then (replace(score, '%', '')::numeric) / 100
else score::numeric
end)
一种方法使用
replace()
:
如果您确实想将其转换为介于0和1之间的数字,而不是0和100之间的数字,请尝试使用案例
:
select (case when right(score, 1) = '%'
then (replace(score, '%', '')::numeric) / 100
else score::numeric
end)
百分比不是数字格式,所以不能直接转换为十进制。百分比不是数字格式,所以不能直接转换为十进制。