How to get only year as part of date / time in SQL Server

When I create a date type in SQL, the result will be 2012-2-1 Monday 00:00:00 something like this: 2012-2-1 Monday 00:00:00

However, I want only a year.

How can i get this?

+4
source share
5 answers

Make a numeric field in just a year or use the YEAR () function.

+7
source

datetime, includes date and time, "date" includes only date formats.

 select datepart(year,getdate()) 
+2
source

You can use this query to get the current year.

 select year(now()); 
+2
source

Could you use the following ?:

 SELECT * FROM table_name WHERE CONVERT(DATE, column_name) >= '2017-01-01' AND CONVERT (DATE, column_name) <= '2017-03-31'; 

But you probably have to use only CONVERT , as in the above query you will get data between two specific dates.

This should only be the part of the DATE that causes the problems, as well as how you inserted the data. If you entered dates in the format " YYYY-MM-DD ", it should work correctly.

+2
source

It is not clear, you just want to choose a year?

if you use mysql you can do something like

 SELECT DATE_FORMAT(<your field name>, '%Y') FROM <your table name> WHERE 1 
0
source

All Articles