根据日期得到星期的函数

--最直接的方式就是用case when判断,这里用表变量来处理的

Go

--创建函数

create function [dbo].[m_getweek](@date nvarchar(2000))  

returns varchar(2000)

AS

begin

    declare @weekday nvarchar(300)

    declare @table table (id int ,weekday nvarchar(200))

    insert into @table

    select 0,'星期天' union select 1,'星期一' union select 2,'星期二' union

    select 3,'星期三' union select 4,'星期四' union select 5,'星期五' union select 6,'星期六'

    select @weekday=weekday from @table where id=(datepart(dw,@date)-1)

    return @weekday

end

 

--测试示例

select [dbo].[m_getweek](getdate()) as 星期

 

--今天的星期

/*

星期

--------

星期六

*/

原文地址:https://www.cnblogs.com/accumulater/p/6244607.html