This topic contains and tries to cover following subjects
- Explanation of DATENAME() function in SQL
- Syntax of DATENAME() function in SQL
- Example to DATENAME() function in SQL
Articles contains and tries to provide answer to following questions and issues:
- What is DATENAME() function..
- How to use DATENAME() function..
- How to select a particular part of datetime value in SQL..
- How to return a date as name from its value..
Articles may require following knowledge:
- Basic query flow in SQL
- SQL Server Management Studio
- SQL server date functions
Explanation of DATENAME() function in SQL
Datename is a built-in date time date function of SQL. It can be used to return a name or a part of a datetime type variable. When we have a datetime type value, and we need a specific part of it: for example only the year name or name of month from its number, we can use datename function.
Idea behind the function is to provide easy access to a datetime value to return one of its part.
This function has a default value, and returns 1900-01-01 if specified part does not exist in datetime data.
Syntax of DATENAME() Function in SQL
Datename function accepts two parameter in its syntax.
1st parameter: which part we need
2st parameter: the datetime type value
DATENAME ( - name of part - , - datetime value - )
DATENAME(year,'00:00:00.000')
Example of DATENAME() Function in SQL
Following example demonstrates how to return a part of a date using datename function. We will use SELECT to return the year from a datetime value.
Query is as follows: