DATENAME() – Date Related Functions

I am untiring in my creation of short articles or tidbits on Transaction SQL date/time functions.

Most, if not all, modern day computer systems collect data which contains dates and times. Many times you will want to convert the data from one data type to another.

Today, I will be exploring the DATENAME() function. This function has 15 different input parameters that can be passed to change the resulting output.

In short, the function takes a date part and date/time variables as input returns the part that you want a character string data.

I will be exploring the seven date part parameters that convert the date variable to a string.

DATE PART PARAMETER TO USE
YEAR YYYY or YY
QUARTER QQ or Q
MONTH MM or M
DAY OF YEAR DY or Y
DAY DD or D
WEEK WK or WW
DAY OF WEEK DW or W

 

The examples below are sample calls to the function. The inputs puts are valid date, null value and a empty string.

The output from the test is listed below. It is surprising that an empty string returns a selected default value.

To make a long story short, pass one of the seven date part parameters to extract the data in a string format.

Related posts

Leave a Comment