Cast string to number, interpreting null or empty string as 0
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a Postgres table with a string column carrying numeric values. I need to convert these strings to numbers for math, but I need both NULL values as well as empty strings to be interpreted as 0 . I can convert empty strings into null values : # select nullif('',''); nullif -------- (1 row) And I can convert null values into a 0 : # select coalesce(NULL,0); coalesce ---------- 0 (1 row) And I can convert strings into numbers : # select cast('3' as float); float8 -------- 3 (1 row) But when I try to combine these techniques, I get errors: