Back to all posts

What difference between the DATEPART() and DATENAME() functions?

The difference between the DATEPART() and DATENAME() functions lies in their definitions. DATEPART() returns an integer that represents the specified datep…

The difference between the DATEPART() and DATENAME() functions lies in their definitions.

DATEPART() returns an integer that represents the specified datepart of the given date, while DATENAME() returns a character string that represents the specified datepart of the given date. In summary, the primary variance between these two functions is the return type:

  • DATEPART() returns an integer.
  • DATENAME() returns a string.
SQL
--When working with dates in SQL Server, sometimes you might find yourself reaching for the DATEPART() function, only to realise that what you really need is the DATENAME() function. Then there may be other situations where DATEPART() is actually preferable to DATENAME().

--So what’s the difference between the DATEPART() and DATENAME() functions?

--Let’s find out.


--Definitions
--The difference between these two functions is in their definitions:

--DATEPART()
--Returns an integer that represents the specified datepart of the specified date.
--DATENAME()
--Returns a character string that represents the specified datepart of the specified date
--According to their definitions, the only difference between these two functions is the return type:

--DATEPART() returns an integer.
--DATENAME() returns a string.
--So that’s the difference.

SELECT
    DATENAME(weekday, GETDATE()) AS 'DATENAME Weekday',
    DATENAME(month, GETDATE()) AS 'DATENAME Month';

SELECT
    DATEPART(weekday, GETDATE()) AS 'DATENAME Weekday',
    DATEPART(month, GETDATE()) AS 'DATENAME Month';

Keep building your data skillset

Explore more SQL, Python, analytics, and engineering tutorials.