How can i return '0' from query in pgsql, if the row doesn't exist?

I don't completely understand what result you want to get, but if you want to get value from a field from some row in a table and 0 if there's no rows, try:

select coalesce((select field from table limit 1), 0)

if you have some filter condition for table which could return 1 row or nothing, try this query:

select coalesce((select field from table where <your condition>), 0)

SELECT CASE WHEN EXISTS (SELECT 1 FROM table WHERE xxx) THEN 1 ELSE 0 END

But your question talks about exists on a field not on a row. If you're just comparing to NULL, and considering that "if it exists", it'd be:

SELECT CASE WHEN field IS NULL THEN 0 ELSE 1 END FROM table

(And of course, if you actually want '0' the string, just put single quotes around the return values.

Tags:

Postgresql