1

A table in my database (PostgreSQL 9.6) has a mixture of NULL and not null values, which I need to COALESCE() as a part of the creation of another attribute during insert into a resulting dimension table. However, Postgres seems unable to recognize the NULL values as NULL.

SELECT DISTINCT name, description 
FROM my_table 
WHERE name IN('STUDIO', 'ONE BEDROOM') 
 AND description IS NOT NULL;

returns

name description
STUDIO NULL
ONE BEDROOM NULL

Whereas

SELECT DISTINCT name, description 
FROM my_table 
WHERE name IN('STUDIO', 'ONE BEDROOM') 
 AND description IS NULL;

returns

name description

as such, something like

SELECT DISTINCT name, COALESCE(description, 'N/A') 
FROM my_table 
WHERE name IN('STUDIO', 'ONE BEDROOM');

will return

name coalesce
STUDIO NULL
ONE BEDROOM NULL

instead of the expected

name coalesce
STUDIO N/A
ONE BEDROOM N/A

The DDL for these attributes is fairly straightforward:

...
    name text COLLATE pg_catalog."default",
    description text COLLATE pg_catalog."default",
...

I've already checked whether the attribute was filled with 'NULL' rather than being an actual NULL value, and that's not the case. I've also tried quoting the attribute in question as "description" and that hasn't made a difference. Casting to VARCHAR hasn't helped (I thought it might be the fact that it's a TEXT attribute). If I nullify some values in the other text column (name) I'm able to coalesce with a test value, so that one is seemingly behaving as expected leading me to think it's not a data type issue. This table exists in multiple databases on multiple servers and exhibits the same behavior in all of them.

I've tried inserting into a new table that has different attribute definitions:

...

    floorplan_name "character varying(128)" COLLATE pg_catalog."default" NOT NULL DEFAULT 'Unknown'::character varying,
    floorplan_desc "character varying(256)" COLLATE pg_catalog."default" NOT NULL DEFAULT 'Not Provided'::character varying,
...

resulting in

name coalesce
STUDIO NULL
ONE BEDROOM NULL

so, not only is the default value unable to populate, leaving the values NULL in an attribute that is defined as NOT NULL, but the example SELECT statements above all behave in exactly the same way when run against the new table.

Does anyone have any idea what might be causing this?

9
  • What does pg_dump of the table produce? Commented Aug 25, 2022 at 17:09
  • If I copy out to a csv it looks like it's doing nulls as ,"", instead of the usual ,, - is that what you're looking for? Commented Aug 25, 2022 at 17:40
  • community_id,year,week,number,name,description,beds,baths 55bd981ff-ad4d-4e80-b70e-7d372761e840,2022,1,2,STUDIO,"",0,1.00 5bd981ff-ad4d-4e80-b70e-7d372761e840,2022,1,3,STUDIO,"",0,1.00 5bd981ff-ad4d-4e80-b70e-7d372761e840,2022,1,5,STUDIO,"",0,1.00 5bd981ff-ad4d-4e80-b70e-7d372761e840,2022,1,6,STUDIO,"",0,1.00 Commented Aug 25, 2022 at 17:42
  • 1
    What happens if you do select length(description);? If you get 0 then you have empty strings. If you get NULL then you have NULL values. I'm betting on empty strings. In STUDIO NULL are you just saying 'NULL' because you don't see a value? Commented Aug 25, 2022 at 18:46
  • 1
    Seems to be a Azure Data Studio issue. Word to the wise, use psql to confirm query behavior. psql is written by the same folks that write the server code, so it is the best way to test whether something is working or not. Commented Aug 25, 2022 at 22:08

1 Answer 1

1

It turns out that the source database is writing empty strings instead of proper NULLs. Adding NULLIF(description, '') before trying to COALESCE() solves the problem.

Thanks to everyone!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.