Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres - How to check for an empty array

I'm using Postgres and I'm trying to write a query like this:

select count(*) from table where datasets = ARRAY[] 

i.e. I want to know how many rows have an empty array for a certain column, but postgres doesn't like that:

select count(*) from super_eds where datasets = ARRAY[]; ERROR:  syntax error at or near "]" LINE 1: select count(*) from super_eds where datasets = ARRAY[];                                                              ^ 
like image 209
Amandasaurus Avatar asked Apr 10 '09 13:04

Amandasaurus


People also ask

How do you check if an array is empty?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

How do I check if a list is empty in SQL?

You can just use a conditional WHERE clause like so: with t as ( <your query here> ) select t.id, t.name, t. randomid, trand.name as randomname from t left join t trand on t. randomid = trand.id where @ids IS NULL OR t.id IN (select item from dbo.

Is null or empty Postgres?

Oracle reads empty strings as NULLs, while PostgreSQL treats them as empty. Concatenating NULL values with non-NULL characters results in that character in Oracle, but NULL in PostgreSQL. Oracle and PostgreSQL behave similarly in many cases, but one way they differ is in their treatment of NULLs and empty strings.


1 Answers

The syntax should be:

SELECT      COUNT(*) FROM      table WHERE      datasets = '{}' 

You use quotes plus curly braces to show array literals.

like image 156
Tom H Avatar answered Sep 21 '22 17:09

Tom H