SQL DATENAME Function Use and Examples

Overview

The DATENAME function returns a string with the part specified in the function of the date used.

Explanation

Syntax

DATENAME(datepart, datetime)

Parameters

  • datepart – It is the part of the date that we to get. It can be a year (yy, yyyy), quarter (qq, q), month (mm, m), dayofyear(dy, y), day (dd, d), week (wk, ww), weekday (dw, w), hour (hh), minute (mi, n), second (ss, s), millisecond (ms), microsecond (mcs), nanosecond (ns), TZoffset (tz), ISO_WEEK (ISOWK,ISOWW). You can use the full name or the abbreviations in parenthesis.
  • datetime – Is the datetime, date or time used in the function to return part of it.

Simple DATETIME Example

The following example will show the year of the datetime specified.

SELECT DATENAME(year, '2020-03-05 2:10:30.123') as datename
DATENAME simple example

NULL Values with DATENAME

If a null parameter is used, the function will return a null value.

SELECT DATENAME(month, NULL) as datename
datename null values

Conversion failed when converting date for DATENAME

A typical error is a conversion failure. The following code shows an example:

SELECT DATENAME(day,'2021, March 21')

The date is literal and a literal date is not supported. The error message displayed will be the following.

Conversion failed when converting date and/or time from character string.

DATENAME with a Table Data

The following example shows the year, month and day of birthdates of the employee table.

SELECT DATENAME(YY, BirthDate) as year, DATENAME(MM, BirthDate) as month, DATENAME(dd, BirthDate) as day
FROM HumanResources.Employee
DATENAME and a table

DATENAME Example with Different Units

The following example shows the datename using different units.

SELECT 
DATENAME(yy, '2020-03-05 2:10:30.123') as YEAR,
DATENAME(mm, '2020-03-05 2:10:30.123') as MONTH,
DATENAME(DD, '2020-03-05 2:10:30.123') as DAY,
DATENAME(hh, '2020-03-05 2:10:30.123') as HOUR,
DATENAME(mi, '2020-03-05 2:10:30.123') as MINUTE,
DATENAME(ss, '2020-03-05 2:10:30.123') as SECOND,
DATENAME(ms, '2020-03-05 2:10:30.123') as MILLISECOND
DATENAME units used

Here is another example where we use the date below and return all possible output.

DECLARE @date datetime2 = '2021-01-07 14:36:17.6222691'
DateGroupDatePartQueryResult
daydSELECT DATENAME(d, @date)7
daydaySELECT DATENAME(day, @date)7
dayddSELECT DATENAME(dd, @date)7
dayofyeardayofyearSELECT DATENAME(dayofyear, @date)7
dayofyeardySELECT DATENAME(dy, @date)7
dayofyearySELECT DATENAME(y, @date)7
hourhhSELECT DATENAME(hh, @date)14
hourhourSELECT DATENAME(hour, @date)14
microsecondmicrosecondSELECT DATENAME(microsecond, @date)622269
microsecondmcsSELECT DATENAME(mcs, @date)622269
millisecondmillisecondSELECT DATENAME(millisecond, @date)622
millisecondmsSELECT DATENAME(ms, @date)622
minutemiSELECT DATENAME(mi, @date)36
minuteminuteSELECT DATENAME(minute, @date)36
minutenSELECT DATENAME(n, @date)36
monthmSELECT DATENAME(m, @date)January
monthmmSELECT DATENAME(mm, @date)January
monthmonthSELECT DATENAME(month, @date)January
nanosecondnanosecondSELECT DATENAME(nanosecond, @date)622269100
nanosecondnsSELECT DATENAME(ns, @date)622269100
quarterqSELECT DATENAME(q, @date)1
quarterqqSELECT DATENAME(qq, @date)1
quarterquarterSELECT DATENAME(quarter, @date)1
secondsSELECT DATENAME(s, @date)17
secondsecondSELECT DATENAME(second, @date)17
secondssSELECT DATENAME(ss, @date)17
weekweekSELECT DATENAME(week, @date)2
weekwkSELECT DATENAME(wk, @date)2
weekwwSELECT DATENAME(ww, @date)2
weekdaydwSELECT DATENAME(dw, @date)Thursday
weekdaywSELECT DATENAME(w, @date)Thursday
weekdayweekdaySELECT DATENAME(weekday, @date)Thursday
yearyearSELECT DATENAME(year, @date)2021
yearyySELECT DATENAME(yy, @date)2021
yearyyyySELECT DATENAME(yyyy, @date)2021
TZoffsetTZoffsetSELECT DATENAME(TZoffset, @date)+00:00
TZoffsettzSELECT DATENAME(tz, @date)+00:00
ISO_WEEKISO_WEEKSELECT DATENAME(ISO_WEEK, @date)1
ISO_WEEKISOWKSELECT DATENAME(ISOWK, @date)1
ISO_WEEKISOWWSELECT DATENAME(ISOWW, @date)1

Difference Between DATENAME and DATEPART

The first example will work with DATENAME.

SELECT 'The year is: ' + DATENAME(yy, '2020-03-05') as example
CONTACT datename

However, this will fail with DATEPART.

SELECT 'The year is: ' + DATEPART(yy, '2020-03-05') as example

The error message displayed is the following.

Conversion failed when converting the varchar value 'The year is: ' to data type int.

Additional Information

Leave a Reply

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