Paul Kirkason
Paul Kirkason

Reputation: 247

Converting a datetime to return only the year

I am wanting to take a timestamp and return the year only via SQL. My current sql

Convert(varchar(20),IRPBestPracticeTopics.TimeStamp, 106)

returns i.e.

06 Dec 2012

I only want the year to return i.e.

2012

I know it is the number at the end of the conversion (106), does anyone know what number to put into the end of the conversion to return the year?

Thanks in advance!

Upvotes: 0

Views: 72

Answers (3)

BAdmin
BAdmin

Reputation: 937

You can try these,

SELECT YEAR(GETDATE()) AS YR
SELECT DATEPART(YY, GETDATE()) AS YR

Upvotes: 0

Peter H
Peter H

Reputation: 901

MySQL/SQL Server:

select YEAR(thetimestampcolumn) from yourtable

Access/SQL Server:

select DatePart(year, thetimestampcolumn) from yourtable

Oracle:

select TO_CHAR(thetimestampcolumn, 'YYYY') from yourtable

Upvotes: 0

gvee
gvee

Reputation: 17161

SQL Server has a function called... Year() !

SELECT Year(your_date) As the_year

Alternatively there's DatePart()

SELECT DatePart(yy, your_date) As the_year

Upvotes: 2

Related Questions