How to Get the Date from a DateTime Column in SQL

How to Get the Date from a DateTime Column in SQL

Do you need to get the date from a DateTime column in your database? It's easy to do with the help of SQL! With the right query, you can quickly and easily extract the date from a DateTime column and use it for whatever purpose you need.

The Solution

The solution is to use the DATE() function. This function takes a single argument, which is the DateTime column you want to extract the date from. For example, if you wanted to extract the date from the created_at column of your database, you would use the following query:

SELECT DATE(created_at) FROM table_name;

Examples

Let's look at a few examples of how this function can be used. Suppose you have a table called users with the following data:


name      created_at
------------------------
John      2020-01-01 12:00:00
Jane      2020-02-01 11:00:00
John      2020-03-01 10:00:00
Bob       2020-04-01 09:00:00

If you wanted to extract the date from the created_at column, you would use the following query:

SELECT DATE(created_at) FROM users;

This query would return the result 2020-01-01, 2020-02-01, 2020-03-01, and 2020-04-01, since these are the dates associated with each record in the table.

Additional Info

The DATE() function is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database. 🤓

Want to build your own LLM Apps with AirOps👇👇