SQL中在日期范围内判断星期几,把周六、周日的日期插入表

–创建表

CREATE TABLE [dbo].[WeekendDate](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Date] [datetime] NULL,
    [Weekday] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]

–将周六日的数据插入到表

declare @datetime datetime,@weekday char(12)

set @datetime='2012-01-01'
while @datetime<='2017-01-01'

begin

select @weekday=datename(weekday,@datetime)
if @weekday= 'Saturday' insert WeekendDate([date],[weekday]) values(@datetime,@weekday)
if @weekday= 'Sunday' insert WeekendDate([date],[weekday]) values(@datetime,@weekday)
--select @datetime,@weekday
select @datetime=dateadd(day,1,@datetime)
end

(如果系统为中文,则把Saturday改为星期六,Sunday改为星期日)

–检查插入结果

--select * from weekenddate