Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql, select empty fields

Tags:

postgresql

I'm trying to get empty "text" fields from my table which I cleared manually with pgadmin. Initially in those fields was '' and I can query them like this:

SELECT mystr, mystr1 FROM mytable WHERE mystr='' or mystr1=''

But that not work if I delete text from them and leave cells blank.

How to write query to get those '' and clear cells together in result? Or clear cells alone?

like image 465
Wine Too Avatar asked Jan 05 '13 14:01

Wine Too


People also ask

How do I SELECT NULL values in PostgreSQL?

Example - With INSERT Statement INSERT INTO contacts (first_name, last_name) SELECT first_name, last_name FROM employees WHERE employee_number IS NULL; This PostgreSQL IS NULL example will insert records into the contacts table where the employee_number contains a NULL value.

Is empty in PostgreSQL?

Oracle reads empty strings as NULLs, while PostgreSQL treats them as empty.

How do I empty a column in PostgreSQL?

Syntax. The syntax to drop a column in a table in PostgreSQL (using the ALTER TABLE statement) is: ALTER TABLE table_name DROP COLUMN column_name; table_name.

What is <> in PostgreSQL?

PostgreSQL is a powerful, open source object-relational database system that uses and extends the SQL language combined with many features that safely store and scale the most complicated data workloads.


1 Answers

SELECT mystr, mystr1 
FROM mytable 
WHERE COALESCE(mystr, '') = '' 
   OR COALESCE(mystr1, '') = ''
    ;

Explanation: the coalesce(a,b,c, ...) function traverses the list a,b,c,... from left to right and stops at the first non-null element. a,b,c can be any expression (or constant), but must yield the same type (or be coercable to the same type).

like image 142
wildplasser Avatar answered Oct 21 '22 08:10

wildplasser