Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Postgresql function return integer or null

I have a function like this, which checks for appropriate account type:

CREATE OR REPLACE FUNCTION acc_is_exclusive(acc_no integer, acc_type char) RETURNS integer AS $$
  BEGIN
    RETURN (SELECT 1 FROM account WHERE account_no = acc_no AND account_type = acc_type);
  END;
$$ LANGUAGE plpgsql;

Return type is integer, and if no record found its going to be NULL instead. Does it make sense at all? Or i need to somehow explicitly declare that return type is going to be integer or null?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You could use EXISTS logic here:

CREATE OR REPLACE FUNCTION acc_is_exclusive(acc_no integer, acc_type char) RETURNS boolean AS $$
    BEGIN
        RETURN EXISTS (SELECT FROM account WHERE account_no = acc_no AND account_type = acc_type);
    END;
$$ LANGUAGE plpgsql;

This would return either true or false, depending on whether or not a matching record can be found.

over 4 years ago · Santiago Trujillo Report

0

The SQL standard says:

Every data type includes a special value, called the null value, sometimes denoted by the keyword NULL.

So it goes without saying that you can return NULL as an integer result, because NULL is a valid value for any data type.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!