SQL NULL state

I'm not a SQL developer

I had no choice but to use Direct Database Query to query my records, but I just know a few about writing SQL

My goal : be able to ignore empty values

I've read about coalesce to help me achieve that and here is the statement:

...
AND cashier = coalesce(?::integer, cashier)

Then I'm supposed to send the argument value

But it seems that when cashier == null , it is sending 'null' to the SQL instead of NULL , so my statement never work.

Then I got and idea, create a function(with the help of gpt) that handle that. Here it is:

CREATE FUNCTION nuller(input_value ANYELEMENT)
RETURNS TEXT AS $$
BEGIN
    IF input_value IS NULL OR input_value = 'null' OR input_value = '0' OR input_value = 0 OR input_value = '' THEN
        RETURN NULL;
    ELSE
        RETURN input_value;
    END IF;
END;
$$ LANGUAGE plpgsql;

Tested it and got Syntax error: 7 ERROR: cannot insert multiple commands into a prepared statement

I'm definitely stucked now. Can anyone please help?

12 replies