1

can you tell me how to use CASE and IFNULL in postgre? i'm migrating from sql to postgreSQL, and i want to make a function with this syntax :

SELECT a.furniture_id, a.furniture_brand, a.furniture_cost, 
CASE 
  WHEN furniture_id = f_id THEN a.furniture_content 
  ELSE '' 
END CONTENT 
   FROM tb_furniture a
   WHERE 
    (IFNULL(a.sale_id,0) = IFNULL(f_id,0) OR (a.furniture_id = f_Id AND IFNULL(a.furniture_content,'') > '')) 
    AND a.published = 1;

thanks before(y)

1
  • 3
    Er, PostgreSQL is SQL. Do you mean "I'm migrating from Microsoft SQL Server to PostgreSQL"? Commented Jul 8, 2015 at 4:19

1 Answer 1

3

This seems to be what you are after, but carefully check the WHERE conditions:

SELECT a.furniture_id, a.furniture_brand, a.furniture_cost, 
       CASE WHEN a.furniture_id = f_id
       THEN a.furniture_content 
       ELSE '' 
       END AS content
FROM tb_furniture a
WHERE (coalesce(a.sale_id,0) = coalesce(f_id,0) OR length(content) > 0)
  AND a.published = 1;
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think there is an IFNULL in PostgreSQL, you'd want to use the standard COALESCE or skip the conversion and go to IS DISTINCT FROM or IS NOT DISTINCT FROM.
Yeah. Obvious mistake. Corrected.
@AnitaWulandari Since f_id is a parameter and used both in the CASE statement and in the WHERE condition, the latter can be simplified. See updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.