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 6718705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:00:10+00:00 2026-05-26T09:00:10+00:00

SELECT ARRAY[1,2,3] – ARRAY[5,NULL,6] I am using contrib _int.sql package for array operations in

  • 0

SELECT ARRAY[1,2,3] – ARRAY[5,NULL,6]

I am using contrib _int.sql package for array operations in postgresql 8.4
In the above query there is a NULL in right hand side array. Because of this NULL value, it throws an error:

"ERROR:  array must not contain nulls"

Can anyone help me to remove the null values from the array?

  • 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-26T09:00:11+00:00Added an answer on May 26, 2026 at 9:00 am

    1) Arrays can contain NULL values in PostgreSQL 8.4+

    db=# SELECT ARRAY[5,NULL,6];
       array
    ------------
     {5,NULL,6}
    

    2) But you cannot subtract one ARRAY from another in standard PostgreSQL 8.4.

    db=# SELECT ARRAY[1,2,3] - ARRAY[5,NULL,6];
    ERROR:  operator does not exist: integer[] - integer[]
    

    3) You can do that in PostgreSQL 8.4 with the contrib package intarray installed.

    4) But you cannot subtract arrays containing NULL values.

    5) You can also subtract arrays in Ruby. See here in the manual, or here on SO.


    Solution to replace NULLs in an integer array in PostgreSQL:

    Postgres 9.3 or later has array_replace(anyarray, NULL, anyelement) for any array. The manual.

    In older versions:

    CREATE OR REPLACE FUNCTION f_int_array_replace_null (int[], int)
    RETURNS int[] AS
    $$
    SELECT ARRAY (
        SELECT COALESCE(x, $2)
        FROM   unnest($1) x);
    $$ LANGUAGE SQL IMMUTABLE;
    

    unnest() was introduced with PostgreSQL 8.4
    For older versions you can use generate_series():

    CREATE OR REPLACE FUNCTION f_int_array_replace_null (int[], int)
    RETURNS int[] AS
    $$
    SELECT ARRAY (
        SELECT COALESCE($1[i], $2)
        FROM   generate_series(1, array_upper($1, 1)) x(i));
    $$ LANGUAGE SQL IMMUTABLE; 
    

    Call:

    event=# SELECT f_int_array_replace_null (ARRAY[5,NULL,6], 0);
     f_int_array_replace_null
    --------------------------
     {5,0,6}
    

    Disclaimer: both versions are not fit for multidimensional arrays.

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

Sidebar

Related Questions

I have an array of countries that I will be using in a select
I have this php code $jsonArray = array(); $sql = SELECT ID,CLIENT FROM PLD_SERVERS;
Is there any fast way to flatten an array and select subkeys ('key'&'value' in
Query 1: SELECT ARRAY(select id from contacts where id = 0)::INT[], ARRAY[]::INT[], ARRAY(SELECT id
How do I accumulate values in T-SQL? AFAIK there is no ARRAY type. I
Is there a way to check a CASE when it's in an array: SELECT
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
$result = mysql_query(SELECT * FROM project ORDER BY projectid); while($row = mysql_fetch_array($result)) { return(array($row['projectid'],
I have a problem with Hibernate Native Query. I have one SELECT that selects
is there any mistake in this validation??? var $validate = array( 'brand_id' => array(

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.