Home >Database >Mysql Tutorial >How to Get the Day of the Week in SQL Server?
SQL Server: Extracting the Day of the Week
Need to determine the day of the week for a date in SQL Server? This guide outlines two efficient methods using built-in functions.
Method 1: Using DATENAME
The DATENAME
function provides a user-friendly textual representation of a date part. To get the day of the week, use the "dw" parameter. For example, this query returns the day of the week for the current date:
<code class="language-sql">SELECT DATENAME(dw, GETDATE())</code>
Method 2: Using DATEPART
Alternatively, DATEPART
returns an integer representing the date part. With "dw", it returns a value between 1 (Sunday) and 7 (Saturday). This query shows the numerical representation of the current day:
<code class="language-sql">SELECT DATEPART(dw, GETDATE())</code>
Both DATENAME
and DATEPART
effectively retrieve the day of the week. Choose the function that best suits your needs: DATENAME
for text ("Monday", "Tuesday", etc.), and DATEPART
for a numerical representation (1-7). Both offer simple and robust solutions for this common SQL Server task.
The above is the detailed content of How to Get the Day of the Week in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!