当前位置:数据库 > SQLServer >>

sql返回星期几的几种方法

sql返回星期几的几种方法
下面介绍由sql来获取当前是星期几的几种实现方法,有需要的朋友可以参考一下。

DECLARE @Dt datetime

SELECT @Dt='2008-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='2011-08-15'
select @weekint= DATEPART(weekday, @Date)
--SELECT @weekint   ---?T聚文 Geovin Du
--select @weekint=DATEPART(weekday, WorkAttendanceDatetime) from WorkAttendanceReport WHERE WorkAttendanceID=1
SELECT @weekint

if @weekint=1
   SET @weekname=N'星期日'
if @weekint=2
 SET @weekname=N'星期一'
if @weekint=3
 SET @weekname=N'星期二'
if @weekint=4
 SET @weekname=N'星期三'
if @weekint=5
 SET @weekname=N'星期四'
if @weekint=6
 SET @weekname=N'星期五'
if @weekint=7
 SET @weekname=N'星期六'
SELECT @weekname

---返回星期??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 --参数
)
returns nvarchar(500)
as
begin
declare @weekint int,@weekname nvarchar(50)
--set @Date='2011-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'星期日'
if @weekint=2
 SET @weekname=N'星期一'
if @weekint=3
 SET @weekname=N'星期二'
if @weekint=4
 SET @weekname=N'星期三'
if @weekint=5
 SET @weekname=N'星期四'
if @weekint=6
 SET @weekname=N'星期五'
if @weekint=7
 SET @weekname=N'星期六'
RETURN @weekname
end
GO

补充:数据库,Mssql
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,