SQL Server Extract Year Month Or Day From DATE Data Type? – 2 simple and useful queries!

You are currently viewing SQL Server Extract Year Month Or Day From DATE Data Type? – 2 simple and useful queries!
Photo by rafzin p on Unsplash
Share This Post, Help Others, And Earn My Heartfelt Appreciation! :)
4.9
(751)

If you used databases like Oracle or Teradata, you can be surprised that very popular function like [ SQL Server extract year month or day from date ] extract(year from date) not work for Microsoft’s databases. In this post I show you how to extract only a day, month or year from Microsoft SQL Server or Azure SQL Database. The both of these databases use Transact-SQL language. (Select Year Sql Server)

SQL Server Extract Year Month Or Day From DATE Data Type

To extract only YEAR, you can use function:

YEAR( your_date ) 

It doesn’t matter if you put attribute with date data type or text in appopriate format:

e.g.:

SELECT YEAR(LOAD_DATE) FROM CUSTOMER;

SELECT YEAR('20191218');

Analogously, for extract MONTH and DAY you can use:

MONTH ( your_date ) 
DAY ( your_date )

If you enjoyed this post please add the comment below and share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

How useful was this post?

Click on a star to rate it!

Average rating 4.9 / 5. Vote count: 751

No votes so far! Be the first to rate this post.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments