

It allows you to format date/time as well as numeric values as strings. This is a more versatile function than the previous ones. We can use the FORMAT() function to return the same values as with the DATENAME() function, and more. DATENAME() also returns month and weekday as their full name, rather than their numerical value.Įxample: DECLARE datetime2 = ' 08:24:14.3112042' įor more examples of this function, see DATENAME() Examples in SQL Server. The DATENAME() function is similar to the DATEPART() function, except that it returns the result as a character string instead of an integer. But not to worry, you can use the DATENAME() or FORMAT() functions for that. The DATEPART() function returns its result as an integer, and therefore, you won’t be able to get the month name or weekday name from the date. For more examples, see DATEPART() Examples in SQL Server. You can also return the various time parts, such as minutes, seconds, milliseconds, etc. As you can see with this example, I returned the week day as well as the day ( day is the day of the month, weekday is the day of the week). One benefit of using this function is that you can also return other parts of the date and time. Therefore, we can use this function to return exactly the same result as the previous example: DECLARE datetime2 = ' 08:24:14.3112042' The DATEPART() function was built specifically for returning specified parts of a date. They return the same result that the DATEPART() function returns for the specified datepart. These functions return the datepart as an integer. Here’s an example of how they work: DECLARE datetime2 = ' AS AS AS YEAR

Yes, T-SQL has functions built specifically for the purpose of returning these three dateparts. The most obvious way to return the day, month and year from a date is to use the T-SQL functions of the same name. In particular, the following functions allow you to return the day, month, and year from a date in SQL Server. Either way, there are plenty of ways to do this in SQL Server. Other times we might want the day of the week. For example, sometimes we only want the year, or the month.

One of the more common tasks when working with dates is to extract the different parts of the date. Transact-SQL includes a bunch of functions that help us work with dates and times.
