I have a column of varchar() type, value can be any string, is it possible to find sum of all rows that can be cast to numeric?
value
----
| 3 |
| 2 2|
| as |
| a1 |
| !2@|
| 0.5|
----
3.5
You can use a case expression and convert:
select (case when value ~ '^[0-9]+[.]?[0-9]*$'
then value::numeric
else 0
end)
from t;
Here is a db<>fiddle.
SUM().