sql
  1. sql-select-date

SQL SELECT Statement - SELECT DATE

The SELECT DATE statement is used to retrieve or display date values from a database table. It helps to extract date values in various formats to perform operations such as comparison, calculation, and formatting of date values.

Syntax

The syntax of the SQL SELECT DATE is as follows:

SELECT DATE(ColumnName) FROM TableName;

Example

Consider a table named "Orders” which contains the following data:

OrderID OrderDate CustomerName ShipperCity
1 2021-01-01 John Smith New York
2 2021-03-15 Mary Johnson Chicago
3 2021-05-20 Peter Wang Los Angeles
4 2021-06-30 David Kim Houston

To retrieve the date from the OrderDate column of the Orders table, the SQL query would be:

SELECT DATE(OrderDate) FROM Orders;

Output

The output of the above SQL query would be:

OrderDate
2021-01-01
2021-03-15
2021-05-20
2021-06-30

Explanation

The SQL SELECT DATE statement selects and returns the date portion of the OrderDate column in the format YYYY-MM-DD.

Use

The SQL SELECT DATE statement is mostly used to extract date values from database tables to perform operations like comparison, calculation, and formatting of date values. It is also useful to display only the date portion of a datetime column.

Important Points

  • The SELECT DATE function can only be applied to datetime columns in the MySQL database.
  • The output of the SELECT DATE statement will always be in the format "YYYY-MM-DD".
  • The SELECT DATE statement is not suitable for querying time or datetime values.

Summary

The SELECT DATE statement is a useful SQL statement to retrieve or display the date values from a database table. It is primarily used to extract date values in various formats to perform operations such as comparison, calculation, and formatting of date values. The SELECT DATE statement is not suitable for querying time or datetime values.

Published on: