如何在SQL Server中的两个WeekDay名称字段之间获取WeekDay名称

时间:2015-09-09 06:23:44

标签: sql-server

我有两个工作日名称列,我的问题是如何在这两个工作日名称之间获取所有工作日名称

前:

 WeekStartDate      WeekEndDate  
   monday             friday

我想要这样的结果

Monday, Tuesday, Wednesday, Thursday, Friday

请帮帮我

谢谢

1 个答案:

答案 0 :(得分:0)

你可以在下面这样做:

declare @tbl table (WeekStartDate  nvarchar(60),WeekEndDate    nvarchar(60) )
insert into @tbl values ('Monday', 'Friday')

declare @weeknums table (name nvarchar(60), num int)
insert into @weeknums values 
 ('Monday'    , 1 ),
 ('Tuesday'   , 2 ),
 ('Wednesday' , 3 ),
 ('Thursday'  , 4 ),
 ('Friday'    , 5 ),
 ('Saturday'  , 6 ),
 ('Sunday'    , 7 )

declare @min nvarchar(max) = (select min(num) 
                              from @tbl t
                              join @weeknums w on t.WeekStartDate = w.name or t.WeekEndDate = w.name)

declare @max nvarchar(max) = (select max(num) 
                              from @tbl t
                              join @weeknums w on t.WeekStartDate = w.name or t.WeekEndDate = w.name)

select w.* 
from @weeknums w
where w.num between @min and @max