Sample Header Ad - 728x90

NULL value causes blank row in SELECT results for text concatenation

8 votes
2 answers
2694 views
I have a query for producing some text for creating some VALUES text to put in a .sql file for inserting some rows. I get a blank line in the results: postgres=# SELECT ' (' || obj_id || ', ''' || obj_type || '''),' FROM il2.objects WHERE obj_id < 11 ORDER BY obj_id; ?column? ------------------------- (1, 'ScienceDomain'), (3, 'PIs'), (10, 'Instrument'), (4 rows) Doing a select *, it's pretty clear it's being caused by the obj_type being NULL for obj_id 2: postgres=# SELECT * FROM il2.objects WHERE obj_id < 11; obj_id | obj_type --------+--------------- 10 | Instrument 1 | ScienceDomain 2 | 3 | PIs (4 rows) (confirming it's NULL): postgres=# SELECT * FROM il2.objects WHERE obj_type IS NULL; obj_id | obj_type --------+---------- 2 | Why is the result of the first SELECT giving me a blank row? Even casting obj_type::text still gave me a blank row. ---------- Additional Info: The schema, for what it's worth: postgres=# \d il2.objects Table "il2.objects" Column | Type | Collation | Nullable | Default ----------+-------------------+-----------+----------+---------------------------------- obj_id | integer | | not null | generated by default as identity obj_type | character varying | | | Indexes: "objects_pkey" PRIMARY KEY, btree (obj_id)
Asked by Randall (385 rep)
Aug 1, 2019, 03:45 PM
Last activity: Aug 15, 2022, 11:41 PM