Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:53:48+00:00 2026-05-26T01:53:48+00:00

I have the following function in Postgres: CREATE OR REPLACE FUNCTION point_total(user_id integer, gametime

  • 0

I have the following function in Postgres:

CREATE OR REPLACE FUNCTION point_total(user_id integer, gametime date)
  RETURNS bigint AS
$BODY$
SELECT sum(points) AS result
  FROM picks
 WHERE user_id = $1
   AND picks.gametime > $2
   AND points IS NOT NULL;
$BODY$
  LANGUAGE sql VOLATILE; 

It works correctly, but when a user starts out and has no points, it very reasonably returns NULL. How can I modify it so that it returns 0 instead.

Changing the body of the function to that below results in an “ERROR: syntax error at or near “IF”.

SELECT sum(points) AS result
  FROM picks
 WHERE user_id = $1
   AND picks.gametime > $2
   AND points IS NOT NULL;

IF result IS NULL
   SELECT 0 AS result;
END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:53:48+00:00Added an answer on May 26, 2026 at 1:53 am

    You need to change the language from sqlto plpgsql if you want to use the procedural features of PL/pgSQL. The function body changes, too.

    Be aware that all parameter names are visible in the function body, including all levels of SQL statements. If you create a naming conflict, you may need to table-qualify column names like this: table.col, to avoid confusion. Since you refer to function parameters by positional reference ($n) anyway, I just removed parameter names to make it work.

    Finally, THEN was missing in the IF statement – the immediate cause of the error message.

    One could use COALESCE to substitute for NULL values. But that only works if there is at least one resulting row. COALESCE can’t fix "no row" it can only replace actual NULL values.

    There are several ways to cover all NULL cases. In plpgsql functions:

    CREATE OR REPLACE FUNCTION point_total(integer, date, OUT result bigint)
      RETURNS bigint AS
    $func$
    BEGIN
    
    SELECT sum(p.points)          -- COALESCE would make sense ...
    INTO   result
    FROM   picks p
    WHERE  p.user_id = $1
    AND    p.gametime > $2
    AND    p.points IS NOT NULL;  -- ... if NULL values were not ruled out
    
    IF NOT FOUND THEN             -- If no row was found ...
       result := 0;               -- ... set to 0 explicitly
    END IF;
    
    END
    $func$  LANGUAGE plpgsql;
    

    Or you can enclose the whole query in a COALESCE expression in an outer SELECT. "No row" from the inner SELECT results in a NULL in the expression. Work as plain SQL, or you can wrap it in an sql function:

    CREATE OR REPLACE FUNCTION point_total(integer, date)
      RETURNS bigint AS
    $func$
    SELECT COALESCE(
      (SELECT sum(p.points)
       FROM   picks p
       WHERE  p.user_id = $1
       AND    p.gametime > $2
       -- AND    p.points IS NOT NULL  -- redundant here
      ), 0)
    $func$  LANGUAGE sql;
    

    Related answer:

    • How to display a default value when no match found in a query?

    Concerning naming conflicts

    One problem was the naming conflict most likely. There have been major changes in version 9.0. I quote the release notes:

    E.8.2.5. PL/pgSQL

    PL/pgSQL now throws an error if a variable name conflicts with a
    column name used in a query (Tom Lane)

    Later versions have refined the behavior. In obvious spots the right alternative is picked automatically. Reduces the potential for conflicts, but it’s still there. The advice still applies in Postgres 9.3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I have following function which returns Table . create Function FN(@Str varchar(30)) returns @Names
I have a stored procedure with the following header: FUNCTION SaveShipment (p_user_id IN INTEGER,
i have following function with a query. it works fine with integer id, but
Suppose we have following function in PHP: public function testSomething() { $name = perform_sql_query("SELECT
I have the following function inside my module. Function Colorize(myValue) ActiveCell.Select Selection.Font.Bold = True
I have the following function that is pulling data from a database. The ajax
So: I have the following function, adapted from a formula found online, which takes
Let's say I have the following function: sumAll :: [(Int,Int)] -> Int sumAll xs
I have the following Oracle function: function get_job_no return number is V_job_no number; begin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.