我正在写一个存储过程。基本上我需要从用户那里获取年份和月份信息并做一些事情。但我的问题是我没有发现给定月份有多少天。让我用例子解释一下;
如我所说,用户给我一年和一个月;
docker run --name cuda_app $(for dev in /dev/nvidia*; do echo -n "--device $dev:$dev "; done) cuda_app
所以我必须创建像
这样的行@year = 2015
@month = 07
我的计划是将这些行逐个添加到另一个临时表中。
我的SP的最后状态是;
2015-07-01
2015-07-02
.....
2015-07-31
我希望能正确解释。
答案 0 :(得分:1)
我已将年和月变量更改为int
数据类型,
您的日期列的名称为dates_date
,并使用convert
和带有dateadd
函数的while循环来填充@dates
表:
ALTER PROCEDURE [dbo].[Usp_CreateStats]
(
@Year int,
@Month int
)
AS
BEGIN
DECLARE @StartTime VARCHAR (10),
@EndTime VARCHAR (10),
@Date date
SET @StartTime = '00:00:00'
SET @EndTime = '23:59:59'
SET @Date = CONVERT(date, right('0000' + cast(@year as char(4)), 4) + right('00' + cast(@month as char(2)), 2)+ '01', 112)
DECLARE @Peoples TABLE (name VARCHAR(50))
INSERT INTO @Peoples (name) select distinct name from USERINFO
DECLARE @Dates TABLE (dates_date date)
while month(@date) = @Month
begin
insert into @Dates (dates_date) values (@Date)
set @Date = dateadd(day, 1, @date)
end
END
答案 1 :(得分:1)
要根据@year
和@month
生成日期,您可以使用Tally Table。
DECLARE @year INT = 2015,
@month INT = 7
DECLARE @startDate DATE = DATEADD(MONTH, @month - 1, DATEADD(YEAR, @year - 1900, CAST('19000101' AS DATE)))
;WITH Tally (n) AS
( -- 100 rows
SELECT TOP(31)
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
)
SELECT
DATEADD(DAY, N-1, @startDate)
FROM Tally t
WHERE
DATEADD(DAY, N-1, @startDate) < DATEADD(MONTH, 1, @startDate)
注意:为变量使用适当的数据类型。在这种情况下,@month
和@year
应为INT
。
答案 2 :(得分:1)
您可以使用递归CTE。
Declare @Year as varchar(5) = '2015'
Declare @Month as varchar(5) = '05'
Declare @DateOfMonth as DateTime = @Month + '-01-' + @Year
Declare @startdate datetime, @enddate datetime
SET @startdate = DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,@DateOfMonth),0))
SET @enddate = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@DateOfMonth)+1,0))
SET @enddate = DATEADD(day,-1,@enddate)
;With DateSequence( Date ) as
(
Select @startdate as Date
union all
Select dateadd(day, 1, Date)
from DateSequence
where Date < @enddate
)
--select result
Select * from DateSequence option (MaxRecursion 1000)
答案 3 :(得分:0)
DECLARE @Month int= 7
Declare @Year int = 2015
Declare @MonthStart date = CAST(@Year as varchar(4)) + Right('00' + Cast(@Month as varchar(2)), 2) + '01'
DECLARE @DaysInMonth int = (SELECT
datediff(day, dateadd(day, 1-day(@MonthStart), @MonthStart),
dateadd(month, 1, dateadd(day, 1-day(@MonthStart), @MonthStart))))
Declare @Dates TABLE (date date)
DECLARE @Counter int = 0
WHILE @Counter < @DaysInMonth
BEGIN
INSERT INTO @Dates SELECT DATEADD(Day, @Counter, @MonthStart)
SET @Counter = @Counter + 1
END
答案 4 :(得分:0)
您可以使用CTE:
DECLARE @year INT = 2015,
@month INT = 7,
@first_day_of_month DATETIME,
@last_day_of_month DATETIME
select @first_day_of_month = cast(cast(@year as varchar(4))+'/'+cast(@month as varchar(2))+'/'+'01' as date)
select @last_day_of_month = dateadd(mm,datediff(mm,0,@first_day_of_month)+1,0)-1
;with mycte as
(
select @first_day_of_month as src
union all
select dateadd(dd,1,src) from mycte
where src < @last_day_of_month
)select * from mycte
答案 5 :(得分:0)
使用递归CTE,
DECLARE @year VARCHAR(4) = '2015',
@month VARCHAR(2)= '02',
@date DATETIME
SET @date= CONVERT(DATETIME, @year + '-' + @month + '-01');
WITH cte
AS (SELECT @date AS dt
UNION ALL
SELECT Dateadd(dd, 1, dt) AS dt
FROM cte
WHERE dt < Dateadd(dd, -1, Dateadd(mm, Datediff(mm, 0, @date) + 1, 0)))
SELECT *
FROM cte
答案 6 :(得分:0)
这是另一个(更快)的计数解决方案:
DECLARE @year INT = 2015,
@month INT = 7
DECLARE @start datetime = dateadd(m, (@year- 1900) * 12 + 7, -31)
SELECT TOP(datediff(d, @start, dateadd(m, 1, @start)))
dateadd(d, ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1, @start)
FROM
(VALUES(0),(0),(0),(0),(0),(0)) a(n)
CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0)) b(n)
答案 7 :(得分:-2)
convert(varchar,OrderDate,111)='2015-07-01'