Dates

Dates


Dates in SQL are often written in the following format:

  • DATE: YYYY-MM-DD
  • DATETIME: YYYY-MM-DD hh:mm:ss

We can use SQL’s date functions to transform data into a desired format. Since date functions can be database specific, verify the functions that exist on your relational database management system.

For example, suppose there’s a bodega table with the following data:

| order_id | item | price | quantity | order_date | |

Dates

DATE()
Converts timestamps to dates (YYYY-MM-DD).
DATETIME()
Returns the both the date and time (YYYY-MM-DD hh:mm:ss).
STRFTIME()
Returns a formatted date.
TIME()
Converts timestamps to time (hh:mm:ss).
Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn SQL on Codecademy