本文介绍了检查日期范围是否有周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我的日期范围有周末,则想要检索'Y',如果日期范围没有周末则想要'N'
Want to retrieve 'Y' if my date range has week end , or 'N' if the date range has no weekend
declare @start datetime;
set @start = '2014-12-21';
declare @end datetime;
set @end = '2014-12-27';
SELECT
-- The code should go here
这应该返回'Y'因为这个日期范围有一个星期结束。 (周六或周日)
This should return 'Y' Because this date range has a week end. (Saturday or Sunday)
推荐答案
Hi Here is your complete solution please check.
Declare @Start DATETIME
declare @end DATETIME
DECLARE @ans nvarchar(1)
SET @Start='2014-12-6'
SET @end='2014-12-15'
WHILE @Start <= @end
BEGIN
if((select DATENAME(dw,@Start))='Saturday' or (select DATENAME(dw,@Start))='Sunday' )
BEGIN
SET @ans='Y'
BREAK
END
ELSE
BEGIN
SET @ans='N'
END
SET @Start=DATEADD(day,1,@Start)
END
PRINT @ans
这篇关于检查日期范围是否有周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!