Home > Database > SQL > body text

How to express not equal to empty in sql

下次还敢
Release: 2024-05-01 23:09:15
Original
1090 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

Use IS NOT NULL query

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

Result:

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

Use <> '' to query

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

Result:

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!