Home  >  Article  >  Database  >  How to express not equal to empty in sql

How to express not equal to empty in sql

下次还敢
下次还敢Original
2024-05-01 23:09:151116browse

There are two ways to write not equal to null in SQL: 1. IS NOT NULL; 2. <> ''. Using the IS NOT NULL query will return records where the column is not NULL, while using the <> '' query will return records where the column is not equal to the empty string.

How to express not equal to empty in sql

How to write not equal to null in SQL

In SQL, not equal to null can use the following two methods The main way to express:

1. IS NOT NULL

<code class="sql">SELECT * FROM table_name WHERE column_name IS NOT NULL;</code>

This query will return the column_name column in the table that is not NULL all records.

2. <> ''

<code class="sql">SELECT * FROM table_name WHERE column_name <> '';</code>

This query will return records in the table where the column_name column is not equal to the empty string.

Example

Using sample table:

<code class="sql">CREATE TABLE table_name (
  id INT NOT NULL,
  name VARCHAR(255)
);</code>

Insert some records:

<code class="sql">INSERT INTO table_name (id, name) VALUES (1, 'John Doe');
INSERT INTO table_name (id, name) VALUES (2, NULL);</code>

Use IS NOT NULL query

<code class="sql">SELECT * FROM table_name WHERE name IS NOT NULL;</code>

Result:

<code>+----+------+
| id | name  |
+----+------+
| 1  | John Doe |
+----+------+</code>

Use <> '' to query

<code class="sql">SELECT * FROM table_name WHERE name <> '';</code>

Result:

<code>+----+------+
| id | name  |
+----+------+
| 1  | John Doe |
+----+------+</code>

The above is the detailed content of How to express not equal to empty in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn