Home  >  Article  >  Database  >  How to use date range in sql

How to use date range in sql

下次还敢
下次还敢Original
2024-05-02 00:30:54770browse

Methods for specifying date intervals in SQL include: using BETWEEN and AND: BETWEEN 'start date' AND 'end date' using >= and <=: 'start date' >= 'end Date' using >= and <= and <: 'Start Date' >= 'End Date', exclude end date

How to use date range in sql

# #Using date intervals in SQL

1. Using BETWEEN and AND

Both BETWEEN and AND operators can be used to specify date intervals. The syntax of BETWEEN is:

<code class="sql">BETWEEN <起始日期> AND <结束日期></code>
The syntax of AND is:

<code class="sql"><起始日期> AND <结束日期></code>
The two operators have the same effect.

2. Use >= and <=

= and <= operators can also be used to specify a date range. The syntax is:
<code class="sql"><起始日期> >= <结束日期></code>
<code class="sql"><结束日期> <= <起始日期></code>

3. Use >= and <

= and < operators can also be used to specify a date range, but it should be noted that , which excludes the end date. The syntax is:
<code class="sql"><起始日期> >= <结束日期></code>

4. Usage example

Query all orders between January 1, 2023 and March 31, 2023:

<code class="sql">SELECT * FROM orders
WHERE order_date BETWEEN '2023-01-01' AND '2023-03-31';</code>
Query all orders before January 1, 2023 or after March 31, 2023:

<code class="sql">SELECT * FROM orders
WHERE order_date < '2023-01-01' OR order_date > '2023-03-31';</code>

The above is the detailed content of How to use date range 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