DATENAME() – Time Related Functions

I am unrelenting 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 four date part parameters that convert the variable to a time string.

DATE PART PARAMETER TO USE
HOUR HH
MINUTE MI or N
SECOND SS or S
TIME ZONE OFFSET TZ

 

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.

A brief summary of this article, pass one of the four ate part parameters to extract the time in a string format.

Related posts

Leave a Comment