SQL has a DateName function, however it takes a datetime as a parameter, not a number

Usage is as follows

DATENAME(month,date) returns Month Name

Sample Query

SELECT DATENAME(month, GETDATE()) AS ‘Month Name’
——————————
Output: January

 

Suppose i want to print previous month date, how can i do. There is a quick way.

print(DATENAME(month,DATEADD (month , -1, getdate() )))

The dateadd DATEADD (month , -1, getdate() ) function deducts one month from current date, then i will use DateName() function to print the previous month date.

Current Month: January, so

Output: December

 

so that’s all for now.. it’s simple for you, just i thought of posting may be useful for somebody..

Enjoy coding..

 

 

 


Discover more from Cloud Distilled ~ Nithin Mohan

Subscribe to get the latest posts sent to your email.

By Nithin Mohan TK

Technology Enthusiast | .NET Specialist | Blogger | Gadget & Hardware Geek

One thought on “DATENAME() function in SQ Server”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.