Skip to content
Advertisement

Extracting data from only the year

I have data in a table in SQL with dates, but how do I select only those that happen in 2021. (The dates look like 31-oct-2020) in the table. The dates are the actual date variable, not just text.

Advertisement

Answer

You should avoid storing your dates as text, but rather should use a proper date column. That being said, you may check the right 4 characters of the date string:

SELECT *
FROM yourTable
WHERE RIGHT(date_col, 4) = '2021';

If the column be an actual date type then use:

SELECT *
FROM yourTable
WHERE date_col >= '2021-01-01' AND date_col < '2022-01-01';
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement