how to extract only the year from the date in sql server 2008?

SqlSql Server-2008DatetimeExtract

Sql Problem Overview


In sql server 2008, how to extract only the year from the date. In DB I have a column for date, from that I need to extract the year. Is there any function for that?

Sql Solutions


Solution 1 - Sql

year(@date)
year(getdate())
year('20120101')

update table
set column = year(date_column)
whre ....

or if you need it in another table

 update t
   set column = year(t1.date_column)
     from table_source t1
     join table_target t on (join condition)
    where ....

Solution 2 - Sql

select year(current_timestamp)

SQLFiddle demo

Solution 3 - Sql

You can use year() function in sql to get the year from the specified date.

Syntax:

YEAR ( date )

For more information check here

Solution 4 - Sql

year(table_column)

Example:

select * from mytable where year(transaction_day)='2013' 

Solution 5 - Sql

SQL Server Script

declare @iDate datetime
set @iDate=GETDATE()

print year(@iDate) -- for Year

print month(@iDate) -- for Month

print day(@iDate) -- for Day

Solution 6 - Sql

the year function dose, like this:

select year(date_column) from table_name

Solution 7 - Sql

DATEPART(yyyy, date_column) could be used to extract year. In general, DATEPART function is used to extract specific portions of a date value.

Solution 8 - Sql

---Lalmuni Demos---
create table Users
(
userid int,date_of_birth date
)
insert into Users values(4,'9/10/1991')

select DATEDIFF(year,date_of_birth, getdate()) - (CASE WHEN (DATEADD(year, DATEDIFF(year,date_of_birth, getdate()),date_of_birth)) > getdate() THEN 1 ELSE 0 END) as Years, 
MONTH(getdate() - (DATEADD(year, DATEDIFF(year, date_of_birth, getdate()), date_of_birth))) - 1 as Months, 
DAY(getdate() - (DATEADD(year, DATEDIFF(year,date_of_birth, getdate()), date_of_birth))) - 1 as Days,
from users

Solution 9 - Sql

Simply use

SELECT DATEPART(YEAR, SomeDateColumn)

It will return the portion of a DATETIME type that corresponds to the option you specify. SO DATEPART(YEAR, GETDATE()) would return the current year.

Can pass other time formatters instead of YEAR like

  • DAY
  • MONTH
  • SECOND
  • MILLISECOND
  • ...etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPraveenView Question on Stackoverflow
Solution 1 - SqlDumitrescu BogdanView Answer on Stackoverflow
Solution 2 - Sqljuergen dView Answer on Stackoverflow
Solution 3 - SqlheretolearnView Answer on Stackoverflow
Solution 4 - SqljkarimView Answer on Stackoverflow
Solution 5 - SqlUJSView Answer on Stackoverflow
Solution 6 - SqlAa.mbiView Answer on Stackoverflow
Solution 7 - SqlVikdorView Answer on Stackoverflow
Solution 8 - SqlLalmuni SinghView Answer on Stackoverflow
Solution 9 - SqlAaron S.View Answer on Stackoverflow