Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在postgresql中将百分比(字符串,带a%)转换为十进制_Sql_Postgresql_Casting - Fatal编程技术网

在postgresql中将百分比(字符串,带a%)转换为十进制

在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

我想对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::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)

百分比不是数字格式,所以不能直接转换为十进制。百分比不是数字格式,所以不能直接转换为十进制。