How to extract only a year from a date in SQL Server 2008?

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

+52
sql datetime sql-server-2008 extract
Sep 15 '12 at 10:51
source share
9 answers
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 .... 
+89
Sep 15 '12 at 10:52
source share
 select year(current_timestamp) 

SQLFiddle demo

+11
Sep 15 '12 at 10:52
source share

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

Syntax:

 YEAR ( date ) 

Check here for more information.

+9
Sep 15 '12 at 11:01
source share
 year(table_column) 

Example:

 select * from mytable where year(transaction_day)='2013' 
+3
Apr 27 '15 at 7:53 on
source share

SQL Server Script

 declare @iDate datetime set @iDate=GETDATE() print year(@iDate) -- for Year print month(@iDate) -- for Month print day(@iDate) -- for Day 
+2
Oct 18 '16 at 8:51
source share

DATEPART (yyyy, date_column) can be used to retrieve the year. In general, the DATEPART function is used to retrieve specific parts of a date value.

0
Sep 15 '12 at 10:53 on
source share
 ---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 
0
Oct 08 '13 at 9:33
source share

Just use

SELECT DATEPART (YEAR, SomeDateColumn)

It will return the part of the DATETIME type that matches the specified parameter. SO DATEPART (YEAR, GETDATE ()) will return the current year.

Can transmit other time formats instead of YEAR, for example

  • DAY
  • MONTH
  • SECOND
  • millisecond
  • ... etc..
0
Aug 04 '16 at 20:53 on
source share

year of dose function like this:

select year(date_column) from table_name

0
Jan 13 '19 at 12:08
source share



All Articles