Several methods for returning the day of the week by SQL

Source: Internet
Author: User
Tags getdate how to use sql

Several methods for returning the day of the week by SQL
The following describes how to use SQL statements to obtain the current day of the week. For more information, see.

DECLARE @ Dt datetime

SELECT @ Dt = '2017-02-21'

Select datepart (wk, @ Dt)

Select datepart (wk, GETDATE ())
Select datepart (weekday, GETDATE ())

SELECT * FROM WorkAttendanceReport
SELECT [dbo]. [GetWeekName] (WorkAttendanceDatetime) FROM WorkAttendanceReport


--- WorkAttendanceDatetime
Declare @ Date smalldatetime, @ weekint int, @ weekname nvarchar (50)
Set @ Date = '2017-08-15'
Select @ weekint = DATEPART (weekday, @ Date)
-- SELECT @ weekint ---? T Juwen Geovin Du
-- Select @ weekint = DATEPART (weekday, WorkAttendanceDatetime) from WorkAttendanceReport WHERE WorkAttendanceID = 1
SELECT @ weekint

If @ weekint = 1
SET @ weekname = n' Sunday'
If @ weekint = 2
SET @ weekname = n' Monday'
If @ weekint = 3
SET @ weekname = n' Tuesday'
If @ weekint = 4
SET @ weekname = n'weday'
If @ weekint = 5
SET @ weekname = n' thurs'
If @ weekint = 6
SET @ weekname = n' Friday'
If @ weekint = 7
SET @ weekname = n' Saturday'
SELECT @ weekname

--- Return the week ?? Br/> if exists (select * from dbo. sysobjects where id = object_id (n' [dbo]. [GetWeekName] ') and xtype in (n'fn', n'if', n'tf '))
Drop function [dbo]. [GetWeekName]
GO
CREATE function GetWeekName

@ Date smalldatetime -- Parameter
)
Returns nvarchar (500)
As
Begin
Declare @ weekint int, @ weekname nvarchar (50)
-- Set @ Date = '2017-08-15'
Select @ weekint = DATEPART (weekday, @ Date)
-- SELECT @ weekint
-- Select @ weekint = DATEPART (weekday, WorkAttendanceDatetime) from WorkAttendanceReport WHERE WorkAttendanceID = 1
-- SELECT @ weekint

If @ weekint = 1
SET @ weekname = n' Sunday'
If @ weekint = 2
SET @ weekname = n' Monday'
If @ weekint = 3
SET @ weekname = n' Tuesday'
If @ weekint = 4
SET @ weekname = n'weday'
If @ weekint = 5
SET @ weekname = n' thurs'
If @ weekint = 6
SET @ weekname = n' Friday'
If @ weekint = 7
SET @ weekname = n' Saturday'
RETURN @ weekname
End
GO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.