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

前端 未结 9 2355
遥遥无期
遥遥无期 2020-12-15 02:09

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?

相关标签:
9条回答
  • 2020-12-15 03:04

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

    Syntax:

    YEAR ( date )
    

    For more information check here

    0 讨论(0)
  • 2020-12-15 03:07
    ---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 讨论(0)
  • 2020-12-15 03:09

    the year function dose, like this:

    select year(date_column) from table_name

    0 讨论(0)
提交回复
热议问题