Compare field with value and return bool

Arman Hayots picture Arman Hayots · Jun 11, 2012 · Viewed 13.8k times · Source

I'm trying move hash cheking from server app to PostgreSQL. In other words I'm need calling a query to PGSQL, which compare string from query with string from field and return result of equal comparison as bool, but I don't know how do this without procedures on clear SQL.

upd: I have table users with field password(currently text, in the future - bytea). I want write sometihng like

select * from values ('WrittenPassword' = users.password) where username = 'someuser' ,

which must return true or false as result of equal comparison.

Answer

Zane Bien picture Zane Bien · Jun 11, 2012

You can use a CASE statement to return certain values based on a condition:

SELECT 
    (CASE WHEN password = 'WrittenPassword' THEN 1 ELSE 0 END) AS is_equal
FROM
    users
WHERE
    username = 'someuser'