Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select only year from a Date Time TSQL

I'm right now creating View and I need to display only the YEAR from a normal DateTime
How can I do that to return just a Year as a value from ? I have already tried it with EXTRACT but somehow it not working .. Thanks for help and fast answer

like image 941
Mingebag Avatar asked Nov 30 '22 21:11

Mingebag


2 Answers

Use YEAR() function

SELECT YEAR(MyDateCol) FROM MyTable

See this SQLFiddle

like image 96
Himanshu Jansari Avatar answered Dec 13 '22 18:12

Himanshu Jansari


You can use DATEPART() to extract the year from datetime value.

DATEPART(YEAR,GETDATE());

Check out SQLFIDDLE

like image 26
Akash KC Avatar answered Dec 13 '22 17:12

Akash KC