SQL Server: Generate all dates for a specified year

Table used for testing: table24

 

Structure and meaning:

field1: primary key serial number

field2: date

field3: the current date is the first week of the year (for query)

field4: Day of the week (such as Monday)

field5: year

 

Create table SQL:

CREATE TABLE [dbo].[table24](
	[field1] [int] IDENTITY(1,1) NOT NULL,
	[field2] [datetime] NULL,
	[field3] [int] NULL,
	[field4] [varchar](10) COLLATE Chinese_PRC_CI_AS NULL,
	[field5] [int] NULL,
 CONSTRAINT [PK_table24] PRIMARY KEY CLUSTERED
(
	[field1] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键-序号' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'table24', @level2type=N'COLUMN', @level2name=N'field1'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'日期' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'table24', @level2type=N'COLUMN', @level2name=N'field2'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The current week of the year' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE' , @level1name=N'table24', @level2type=N'COLUMN', @level2name=N'field3'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'星期几' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'table24', @level2type=N'COLUMN', @level2name=N'field4'

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'年份' ,@level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'table24', @level2type=N'COLUMN', @level2name=N'field5'

 

Generate all dates in a given year:

--test:
--Test default: exec getDateOfThisYear 0,0 ;
--Test the specified year (supplement previous data, do not generate weekend data): exec getDateOfThisYear 2017,1,0;
--Test the specified year (do not supplement previous data, do not generate weekend data): exec getDateOfThisYear 2017,0,0;
--Test the specified year (do not supplement previous data, generate weekend data): exec getDateOfThisYear 2017,0,1;
--Test the specified year (do not supplement previous data, do not generate weekend data): exec getDateOfThisYear 2017,0,0;
--View data: select * from table24;
ALTER PROCEDURE [dbo].[getDateOfThisYear]	
	@cur_year int, -- the current year
	@flag int, --whether to supplement the previous data, 1-supplement, supplement the data before the latest time in the current system 0-no supplement, start directly from the latest time, the default is not to supplement directly from the latest system time Start
	@flag2 int -- whether to generate data for Saturday and Sunday, 1- to generate data for Saturday and Sunday 0- not to generate, the default is not to generate
AS
BEGIN	
	SET NOCOUNT ON;
	if(@cur_year is null or @cur_year<=0)
	begin
		set @cur_year = year(getDate());--assign the current year
	end
	
	if(@flag is null or @flag<0)
	begin
		set @flag = 0;--do not add, start from the latest system time
	end
	
	if(@flag2 is null or @flag2<0)
	begin
		set @flag2 = 0;--do not generate weekend time
	end

	--Starting time	
	declare @startTime varchar(10);
	if(@flag=0)
	begin
		--Get the biggest time of the year in the system
		select top 1 @startTime = dateadd(day,1,field2) from table24 where year(field2)=@cur_year order by field2 desc;--take the first item in descending order and calculate the start time		
		if(@startTime is null)--no data, take the first day of the week, which is Monday
		begin
			set @startTime = dateadd(wk, datediff(wk,0,getdate()), 0);			
		end
	end
	else
	begin--add the previous ones, then start from January 1st
		set @startTime = cast(@cur_year as varchar(20))+'0101';--形如20170101
	end
	
	--End Time
	declare @endTime varchar(10);
	set @endTime = cast((@cur_year+1) as varchar(20))+'0101';--形如20180101

	--Generate data for the current year
	if(@flag2=1)--Generate the time of Saturday and Sunday
	begin
		insert into table24(field2,field5)
		select distinct dateadd(day,number,@startTime),@cur_year
		from master..spt_values
		where number>=0 and number<=365
		and dateadd(day,number,@startTime) <@endTime
		and dateadd(day,number,@startTime) not in (select field2 from table24)--no duplication allowed
	end
	else--do not generate
	begin
		insert into table24(field2,field5)
		select distinct dateadd(day,number,@startTime),@cur_year
		from master..spt_values
		where number>=0 and number<=365
		and dateadd(day,number,@startTime) <@endTime
		and datepart(dw,dateadd(day,number,@startTime)) not in (1,7)--not in (1,7): do not generate data for Saturday and Sunday
		and dateadd(day,number,@startTime) not in (select field2 from table24)--no duplication allowed
	end
	--Update the week and day of each date this year
	update table24 set field3=datepart(week,field2),field4=datename(dw,field2) where field5=@cur_year;

	-- update previous data
	--update table24 set field3=datepart(week,field2),field4=datename(dw,field2),field5=year(field2) where field4 is null or field5 is null;
END
--select datepart(weekday,getdate()) as 'the day of the week'
--select datepart(week,getdate()) as 'the week of the year'
--select datepart(quarter,getdate()) as the quarter of the year'
--select datename(dw,getdate()) as 'the current day of the week' --can return the day of the week in Chinese characters
--select datepart(dw,'20170909') as 'the current day of the week' -- can return the week in digital form (1-Sunday 2-Monday 3-Tuesday and so on)
/*--Generate data for one year
insert tb
 select distinct dateadd(day,number,'20130101')
 from master..spt_values
 where number>=0 and number<=365
 and dateadd(day,number,'20130101') <'20140101'
 and dateadd(day,number,'20130101')  not in (select date from tb)
*/

 

Generate all days of the week:

--Test: exec getDateOfWeek;
CREATE PROCEDURE [dbo].[getDateOfWeek]	
AS
BEGIN	
	SET NOCOUNT ON;
	/*	
	--Get the first day of the current week, which is the Monday of the week
	declare @mon datetime;
	select @mon = dateadd(wk, datediff(wk,0,getdate()), 0);
	*/
	--Get the week of the current week
	declare @weekes int;
	select @weekes = dbo.WeekOfMonth(getDate());
	
	--loop adding data
	declare @count int;
	declare @c int;
	set @count = 6;
	set @c = 0;
	while(@c<=@count)
	begin
		insert into table24(field2,field5)
		select dateadd(wk, datediff(wk,0,getdate()), @c),year(getDate());
		set @c = @c +1;
	end

	update table24 set field3=datepart(week,field2),field4=datename(dw,field2) where field5=year(getDate());
END

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326202877&siteId=291194637