我有一个Postgres表格,其中包含一个字符串列,其携带数字值。我需要将这些字符串转换为数字进行数学计算,但我需要将
我可以将空字符串转换为空值:
我做错了什么?
NULL
值以及空字符串都解释为0
。我可以将空字符串转换为空值:
# select nullif('','');
nullif
--------
(1 row)
我可以将空值转换为0
:
# select coalesce(NULL,0);
coalesce
----------
0
(1 row)
我可以把字符串转换成数字:
# select cast('3' as float);
float8
--------
3
(1 row)
但是当我尝试结合这些技术时,我遇到了错误:
# select cast( nullif( coalesce('',0), '') as float);
ERROR: invalid input syntax for integer: ""
LINE 1: select cast( nullif( coalesce('',0), '') as float);
# select coalesce(nullif('3',''),4) as hi;
ERROR: COALESCE types text and integer cannot be matched
LINE 1: select coalesce(nullif('3',''),4) as hi;
我做错了什么?