Home >Database >Mysql Tutorial >How to Get the Day of the Week from a Date in SQL Server 2005/2008?
Get day of the week in SQL Server 2005/2008
Question:
How to determine the day of the week corresponding to a specific date (such as January 1, 2009) using SQL Server 2005/2008? Is there a built-in function that can perform this task, or do I need to resort to an auxiliary table?
Answer:
SQL Server 2005/2008 provides built-in functions to retrieve the day of the week from a date. You can use DATENAME or DATEPART to achieve this:
Use DATENAME:
<code class="language-sql">SELECT DATENAME(dw, GETDATE()) -- 输出:星期五</code>
In the DATENAME function, "dw" represents the day of the week. It returns a string representing the day of the week, such as "Monday" or "Tuesday".
Use DATEPART:
<code class="language-sql">SELECT DATEPART(dw, GETDATE()) -- 输出:6</code>
DATEPART, on the other hand, returns the day of the week as an integer. Integer values range from 1 to 7, where 1 corresponds to Sunday and 7 corresponds to Saturday.
The above is the detailed content of How to Get the Day of the Week from a Date in SQL Server 2005/2008?. For more information, please follow other related articles on the PHP Chinese website!