在sql server 2008中,如何仅从日期中提取年份。 在DB中我有一个日期列,从中我需要提取年份。 那有什么功能吗?
答案 0 :(得分:98)
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 ....
答案 1 :(得分:12)
select year(current_timestamp)
答案 2 :(得分:10)
答案 3 :(得分:4)
year(table_column)
示例:
select * from mytable where year(transaction_day)='2013'
答案 4 :(得分:2)
SQL Server脚本
declare @iDate datetime
set @iDate=GETDATE()
print year(@iDate) -- for Year
print month(@iDate) -- for Month
print day(@iDate) -- for Day
答案 5 :(得分:0)
DATEPART(yyyy,date_column)可用于提取年份。通常,DATEPART函数用于提取日期值的特定部分。
答案 6 :(得分:0)
---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
答案 7 :(得分:0)
只需使用
SELECT DATEPART(YEAR,SomeDateColumn)
它将返回与您指定的选项对应的DATETIME类型部分。 SO DATEPART(年,GETDATE())将返回当前年份。
可以传递其他时间格式器,而不是像
那样的YEAR答案 8 :(得分:0)
年度功能剂量,例如:
select year(date_column) from table_name