Home >Database >Mysql Tutorial >How to Get the Day of the Week in SQL Server?

How to Get the Day of the Week in SQL Server?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-12 10:19:43497browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn