Home >Database >Mysql Tutorial >How Can I Determine the Day of the Week from a Date in SQL Server?

How Can I Determine the Day of the Week from a Date in SQL Server?

Susan Sarandon
Susan SarandonOriginal
2025-01-12 10:06:41567browse

How Can I Determine the Day of the Week from a Date in SQL Server?

Determine the day of the week in SQL Server

When working with dates in SQL Server, determining the corresponding day of the week is a very common requirement. For example, given the date "January 1, 2009," you might want to determine whether it is Monday, Tuesday, and so on.

Built-in functions

Fortunately, SQL Server provides built-in functions to simplify this task without the need for auxiliary tables.

1. DATENAME

The DATENAME function returns the specified part of the date as a string. To get the day of the week, use the "dw" parameter:

<code class="language-sql">SELECT DATENAME(dw, GETDATE())
-- 星期五</code>

2. DATEPART

The DATEPART function works similarly, but it returns the specified part of the date as an integer. For day of the week, use the "dw" parameter:

<code class="language-sql">SELECT DATEPART(dw, GETDATE())
-- 6</code>

In this case, the integer 6 represents Friday because dates in SQL Server are numbered from 1 (Sunday) to 7 (Saturday).

The above is the detailed content of How Can I Determine the Day of the Week from a Date 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