Reputation: 11
I'm doing my final year project using asp.net as front end and SQL Server 2005 as back end
My question is: I want extract only the year from datetime format. And use the year to filter data
Upvotes: 1
Views: 13917
Reputation: 26498
I agree with Mr. Wheats answer. Just adding a few more possibilities
C#
a) int year = DateTime.Now.Year;
b) Given a date
int year = Convert.ToDateTime("12/28/2010").Year;
VB.Net
Dim year As Int32
year = DateTime.Now.Year
Sql Server 2005
Using DATENAME
a) select [year] = DATENAME(yy,getdate())
b) From a given date select [year] = DATENAME(yy,'12/31/2010')
Using DATEPART
a) select [year] = DATEPART(yy,getdate())
b) From a given date select [year] = DATEPART(yy,'12/31/2010')
Using Year Function(Added after Marc's comment)
select [year] = YEAR (getdate())
Upvotes: 4
Reputation: 300579
In C#:
int year = DateTime.Now.Year;
If you mean at the T-SQL level:
DATEPART ("yyyy", date)
Upvotes: 3