How to Get the Year from a DateTime Column in SQL

To get the year from a DateTime column in your SQL database, use the YEAR() function. This function takes a single argument, which is the DateTime column you want to extract the year from. For example, if you wanted to get the year from the created_at column of your database, you would use the following query:

SELECT YEAR(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:

users
namecreated_at
John2020-01-01
Jane2019-12-31
John2020-01-01
Bob2018-11-30


If you wanted to get the year from the created_at column of this table, you would use the following query:

SELECT YEAR(created_at) FROM users;

This query would return the results 2020, 2019, 2020, and 2018, since these are the years associated with each of the created_at values.

Additional Info

The YEAR() 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👇👇