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

How to use date range in sql

下次还敢
Release: 2024-05-02 00:30:54
Original
837 people have browsed it

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>
Copy after login
The syntax of AND is:

<code class="sql"><起始日期> AND <结束日期></code>
Copy after login
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>
Copy after login
Copy after login
<code class="sql"><结束日期> <= <起始日期></code>
Copy after login

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

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

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template