每月奇数日期MSSQL存储过程实现(mssql 每月奇数日期)

Every month there are odd dates, and if a database needs to store relevant data on certain days, it can be tedious manually. Here, a stored procedure is used to complete the task, which assumes that the database is MSSQL:

First of all, create a stored procedure in the database:

CREATE PROCEDURE getEveryOddDate

@start date date ,

@end date date

AS

BEGIN

declare @curdate date

set @curdate = @start date

while( @curdate

begin

if datepart (dd,@curdate) % 2> 0

begin

INSERT INTO [TabelName]

VALUES(datepart (dd,@curdate),@curdate)

end

set @curdate = dateadd (dd,1,@curdate)

end

end

Then it can be executed:

execute getEveryOddDate “2020-7-3″,”2020-7-30”

When the stored procedure is executed, it will output the odd date from the start date and end date.

For the above example, it will output the odd date from July 3, 2020 to July 30, 2020.

In this way, it can effectively complete the task of collecting the odd dates of each month, and can be applied to many scenarios. For example, collecting the odd dates of each month in a certain year and storing them in the database can be quickly completed.


数据运维技术 » 每月奇数日期MSSQL存储过程实现(mssql 每月奇数日期)