选择过去24(N)个月的第一个日期

时间:2013-02-21 04:34:43

标签: sql sql-server tsql

这是一个显示过去24个月第一次约会的脚本。

  

我需要在单个T-SQL查询中使用以下功能,而不是迭代。

Declare @intCount as int

SET @intCount = 24

Declare @Date as varchar(25)  

While (@intCount >0)

Begin
SET @Date = CONVERT(VARCHAR(25),DATEADD(m,-(@intCount-1),
                                DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101)
select @Date

SET @intCount = @intCount-1

End

上述查询返回24个结果集(select)。但我想要一个结果集

修改

  

主要要求是在子查询中使用此单个结果

6 个答案:

答案 0 :(得分:3)

您可以使用recurcive CTE

;with cte(intCount,myDate)
 as
 (
   Select 1, CONVERT(VARCHAR(25),DATEADD(m,  1,
                            DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101)
   union all
    Select intCount+1 ,CONVERT(VARCHAR(25),DATEADD(m,-(intCount-1),
                            DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101) from cte
                            where intCount<=24
 )
Select myDate from  cte
  

更新:

如果需要,可以将其存储在表变量或临时表

 Declare @Date table
 (myDate varchar(25))

 Declare @count int
 set @count=24
 ;with cte(intCount,myDate)
 as
 (
   Select @count-1, CONVERT(VARCHAR(25),DATEADD(m,-(@count-1),
                            DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101)
   union all
   Select intCount-1 ,CONVERT(VARCHAR(25),DATEADD(m,-(intCount-1),
                            DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101) from cte
                            where intCount>0
 )
 Insert into @Date(myDate) 
 Select myDate from cte

或者您可以创建一个功能

 go
 alter FUNCTION FnGetDate(@intCount int)
 RETURNS  @rtnTable TABLE 
 (
  myDate varchar(25)NOT NULL
 )
 AS
 BEGIN

 ;with cte(level,myDate)
  as
 (
   Select @intCount-1, CONVERT(VARCHAR(25),DATEADD(m,-(@intCount-1),
                        DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101)
   union all
   Select level-1 ,CONVERT(VARCHAR(25),DATEADD(m,-(level-1),
                        DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101) from cte
                        where level>0
 ) 
 Insert into @rtnTable(myDate)
 select myDate from cte
 return
 END

现在你的功能就像

 Select * from dbo.FnGetDate(24)

答案 1 :(得分:1)

如果@intCount不变,您可以通过简单的UNION ALL

来实现
select CONVERT(VARCHAR(25),DATEADD(m,-24, DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101) as date
union all
select CONVERT(VARCHAR(25),DATEADD(m,-23, DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101) as date
union all
.....
select CONVERT(VARCHAR(25),DATEADD(m,0, DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101) as date

或使用临时表:

Declare @intCount as int

SET @intCount = 24

Declare @Date as varchar(25)  

CREATE TABLE #temptable 
    (datefield date)

While (@intCount >0)

Begin
SET @Date = CONVERT(VARCHAR(25),DATEADD(m,-(@intCount-1),
                                DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101)
insert into #temptable                              
select @Date

SET @intCount = @intCount-1

End
select * from #temptable
drop table #temptable

答案 2 :(得分:1)

这就是我的建议,我在存储过程中生锈了,但我将我的建议加入了原始程序

Declare @intCount as int
  CREATE TABLE days (day varchar(25));
 SET @intCount = 24

 Declare @Date as varchar(25)  

 While (@intCount >0)

Begin
 SET @Date = CONVERT(VARCHAR(25),DATEADD(m,-(@intCount-1),
                            DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101)
 INSERT Into days(day) VALUES (@Date)
 SET @intCount = @intCount-1

 End
 SELECT * FROM days;

答案 3 :(得分:1)

使用临时表:

Declare @intCount as int
SET @intCount = 24    
Declare @Date as varchar(25)  
create table #temp1 (myDate date)
While (@intCount >0)
Begin
SET @Date = CONVERT(VARCHAR(25),DATEADD(m,-(@intCount-1),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101)
insert into #temp1
select @Date
SET @intCount = @intCount-1
End

select * from #temp1

答案 4 :(得分:1)

这是一个如何用CTE做的例子:

;WITH DateCTE AS
    (
        SELECT dateadd(dd, - datepart(day, getdate()) + 1, Convert(date, getdate())) AS DateValue
        UNION ALL
        SELECT DATEADD(month, -1, DateValue)
        FROM DateCTE
        WHERE DATEADD(month, 23, DateValue) >  GetDate()
    )
select DateValue from DateCTE;

答案 5 :(得分:0)

DECLARE @intCount AS INT

SET @intCount = 24

DECLARE @Date AS VARCHAR(25)  

WHILE (@intCount > 0)
BEGIN
    SET @Date = ISNULL(@Date, '') + CONVERT(VARCHAR(25),DATEADD(m,-(@intCount-1),
                                DATEADD(dd,-(DAY(GETDATE())-1),GETDATE())),101) + ' '
    --SELECT @Date   Don't have it here
    SET @intCount = @intCount -1
END

SELECT @Date