从当前日期sql server

时间:2017-07-13 10:23:29

标签: sql-server

我需要从下周开始每个星期四开始的日期等等,与当前日期无关(仅需要前26个条目)。

例如,如果今天是星期四(2017年6月13日),那么接下来的日期将是

  
      
  1. (20日至2017年)
  2.   
  3. (6月27日 - 2017年)
  4.   
  5. (2017年8月3日)等等。
  6.   

对于当前任何一天的选择(2017年6月9日至2017年6月15日)(周日至周六),这将是相同的

7 个答案:

答案 0 :(得分:0)

你需要为此设置一些上限。 SQL Server不会返回无限系列。但这应该给你一个想法:

declare @cnt int = 7
declare @thursdays table
(
    thursday date 
)
while @cnt < 100
    begin
        INSERT INTO @thursdays VALUES( cast(DATEADD(dd,@cnt,getdate()) as date))
        set @cnt = @cnt + 7
    end

SELECT * FROM @thursdays

答案 1 :(得分:0)

试试这个

 DECLARE @ThrusdayList TABLE (
    ID INT IDENTITY
    ,DATES DATE
    )
DECLARE @counter INT
DECLARE @Date DATE

SELECT @counter = 0

SELECT @Date = '20170713' --Add your date

WHILE @counter <= 52
BEGIN
    INSERT INTO @ThrusdayList (DATES)
    SELECT Thursday = convert(VARCHAR(11), DATEADD(WEEK, 1, @Date), 120)

    SELECT @counter = @counter + 1

    SELECT @Date = DATEADD(WEEK, 1, @Date)
END

SELECT CONVERT(VARCHAR(11),DATES,113) AS Thursdays
FROM @ThrusdayList

答案 2 :(得分:0)

试试这个;

-- get all dates between 2 points
declare
    @EndDate datetime

set @EndDate=DATEADD(month,6,getdate())

SELECT 
    [date]=convert(datetime, CONVERT(float,d.Seq))
FROM 
    (
    select top 100000 row_number() over(partition by 1 order by A.name) as Seq 
    from syscolumns A, syscolumns B  
    )d 
where
    convert(datetime, CONVERT(float,d.Seq))>GETDATE() and 
    convert(datetime, CONVERT(float,d.Seq))<=@EndDate and
    datename(dw,convert(datetime, CONVERT(float,d.Seq))) ='Thursday'

你需要一个'结束日期',所以添加变量作为一个例子,但有很多方法可以做到这一点

答案 3 :(得分:0)

您可以使用临时计数表生成此日期,如下所示:

 osql -S ABC -U sa -P 123
checking shared odbc libraries linked to isql for default directories...
strings: '': No such file
    trying /tuH ... no
    trying /tmp/sql ... no
    trying /tmp/sql ... no
    trying /etc ... OK
checking odbc.ini files
    reading /home/hadoopuser/.odbc.ini
[ABC] found in /home/hadoopuser/.odbc.ini
found this section:
looking for driver for DSN [ABC] in /home/hadoopuser/.odbc.ini
  no driver mentioned for [ABC] in .odbc.ini
looking for driver for DSN [default] in /home/hadoopuser/.odbc.ini
osql: error: no driver found for [ABC] in .odbc.ini

另一种方法,您也可以使用下面的递归CTE:

Declare @date date = '2017-06-13'

Select top(26) Dates = DateAdd(day, 7*(Row_Number() over (Order by (Select Null))), @date) from
    master..spt_values s1, master..spt_values s2

答案 4 :(得分:0)

使用“7-datepart(dw,@ mydate)+5”查找下周四,然后使用递归部分查找下周四。

declare @mydate datetime = dateadd(day,-4,getdate())

;with Thursdays_rte as
(
    select dateadd(day,7-datepart(dw,@mydate)+5,@mydate) thursday, 1 rn
    union all 
    select dateadd(week,1,thursday),rn+1
    from Thursdays_rte
    where rn < 26
)

select thursday,datename(dw,thursday)
from thursdays_rte

答案 5 :(得分:0)

我采用了Kannan的解决方案并对其进行了修改,以确保您获得了正确的开始日期:

declare @dt date
declare @nDay int
set @dt = '07/12/17'

select @nDay =  datepart(dw,@dt)
print @nDay
IF @nDay > 5 
    SET @dt = DATEADD(day,(@nDay - 5) * -1,@dt)
else
    SET @dt = DATEADD(day, 5 -@nDay ,@dt)

--BECAUSE ROW_NUMBER STARTS AT 1 SET THIS BACK A WEEK TO GET THIS WEEK
SET @dt = DATEADD(day, -7 ,@dt)

Select top(26) Dates = DateAdd(day, 7*(Row_Number() over (Order by (Select Null))), @dt) from
    master..spt_values s1, master..spt_values s2

答案 6 :(得分:0)

    declare @dw int = 5; /*Thursday. put here your day of week as int*/

    with nums as /* generate 26 values */ 
    (
    select number as n
    from master..spt_values
    where type = 'p'
    and number between 0 and 25
    )
    select dateadd(day, 7 * n, start_day)
    from nums cross apply
           (
           select
                   case 
                      when datepart(dw, getdate()) <= @dw
                      then dateadd(day, @dw - datepart(dw, getdate()), cast(getdate() as date))
                      else dateadd(day, 7 +  @dw - datepart(dw, getdate()), cast(getdate() as date))
                   end as start_day /* find the first dw = @dw after today */
           )a;