在sql server 2008中,如何仅从日期中提取年份。在数据库中,我有一列日期,需要从中提取年份。有什么功能吗?
Answers:
year(@date)
year(getdate())
year('20120101')
update table
set column = year(date_column)
whre ....
或者如果您需要在另一个表中
update t
set column = year(t1.date_column)
from table_source t1
join table_target t on (join condition)
where ....
select year(current_timestamp)
年功能剂量,如下所示:
select year(date_column) from table_name
---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