0

嗨,我有一个小功能,我无法确定。我通常会在应用层执行此操作,但没有选项,而且我的 T-SQL 有点生疏。

简化函数:(取一个日期并计算间隔的重复日期)

CREATE FUNCTION [dbo].[fn_GetRunDatesShort] 
      (@startDate DateTime, @weeks INTEGER) 
RETURNS VARCHAR(1000)
AS
BEGIN
   DECLARE @i INTEGER
   DECLARE @interval INTEGER
   DECLARE @outPut VARCHAR(500)

    SET @outPut = ''
    SET @i = 0
    SET @interval = 7

   WHILE (@i < @weeks)
    BEGIN
     SET @output = @outPut + ', ' + 
               Convert(varchar, DATEPART(MONTH, 
                  DATEADD(d, @interval, @startDate)),101) + '-'
             + Convert(varchar, DATEPART(d, 
                  DATEADD(d, @interval, @startDate)))
     SET @interval = @interval + 7
     SET @i = @i + 1
    END
   RETURN @output 
END
GO

SELECT dbo.fn_GetRunDatesShort(GETDATE(), 4)

所以这返回

, 8-21, 8-28, 9-4, 9-11

我需要让这个返回

August 21, 28 | September 4, 11
4

2 回答 2

1

为了避免通过 FOR XML 引擎,我可以建议一些 case 语句结构吗?

CREATE FUNCTION dbo.fn_GetRunDatesShort_2
(
  @StartDate DATE, @Weeks TINYINT
)
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @output VARCHAR(MAX)

SET @output = ''

SELECT @output = @output + CASE @output WHEN '' THEN M ELSE CASE R WHEN 1 THEN ' | ' + M ELSE '' END END + ' ' + CASE R WHEN 1 THEN '' ELSE ', ' END + D
FROM (
    SELECT year(DT) as Y, datename(month,DT) as M, CAST(datepart(day,DT) AS VARCHAR) AS D
        , row_number()over(partition by month(DT), year(dt) order by day(DT)) AS R
        , row_number()over(order by dt) AS R2
    FROM (
        SELECT dateadd(ww,row_number()over(order by object_id), @StartDate) AS DT
        FROM master.sys.objects
    ) z
) y
WHERE R2 <= @weeks
ORDER BY R2

RETURN @output
END
GO

的输出SELECT dbo.fn_GetRunDatesShort_2(GETDATE(),5)

May 26 | June 2 , 9 , 16 , 23

如果日期的年份对输出很重要,也可以对其进行编辑。

于 2014-05-19T19:02:56.540 回答
1

当您可以更有效地生成集合时,请尽量避免循环,并且停止varchar无长度声明。

这是一个结合了您的两个要求的函数(生成日期集并将它们格式化为您尴尬的输出格式):

CREATE FUNCTION dbo.fn_GetRunDatesShort_2
(
  @StartDate DATE, @Weeks TINYINT
)
RETURNS VARCHAR(MAX)
AS
BEGIN
  DECLARE @output VARCHAR(MAX);

  ;WITH cte1(m,d) AS
  (
    SELECT DATENAME(MONTH, wd), CONVERT(VARCHAR(2), DAY(wd)) FROM 
    (
      SELECT TOP (@Weeks) DATEADD(WEEK, ROW_NUMBER() OVER 
          (ORDER BY [object_id]), @StartDate)
      FROM sys.all_objects ORDER BY [object_id]
    ) AS sq(wd)
  ),
  cte2(ds) AS
  (
    SELECT DISTINCT m + STUFF((SELECT ', ' + d FROM cte1 AS cte1_a 
      WHERE cte1_a.m = cte1.m
      FOR XML PATH(''), TYPE).value('.[1]','varchar(max)'),1,1,'') 
      FROM cte1
  )
  SELECT @output = STUFF((SELECT ' | ' + ds FROM cte2 
      FOR XML PATH(''),TYPE).value('.[1]','varchar(max)'),1,3,'');

  RETURN (@output);
END
GO

(如果你有一个 Numbers 表——你应该这样做——你可以将查询替换为sys.all_objects与 Numbers 表相似的查询,然后你可以添加WITH SCHEMABINDING到函数中——这在这种情况下可能没有帮助,但这是我尝试的尽可能始终如一地做。)

用法:

SELECT dbo.fn_GetRunDatesShort_2(GETDATE(), 4);

结果:

August 21, 28 | September 4, 11

如果你的方法是 52,那么问题就来了——如果你的日期是今年 8 月,日期是明年 8@weeks月,你期望什么输出?


编辑 2014-05-19

为确保以正确的方向排序(去年 8 月运行良好,但现在似乎在 5 月更改了顺序),您可以对函数进行以下更改:

CREATE FUNCTION dbo.fn_GetRunDatesShort_2
(
  @StartDate DATE, @Weeks TINYINT
)
RETURNS VARCHAR(MAX)
AS
BEGIN
  DECLARE @output VARCHAR(MAX);

  ;WITH cte1(mn,m,d) AS
  (
    SELECT 
      DATEPART(YEAR, wd) * 100 + DATEPART(MONTH, wd), 
      DATENAME(MONTH, wd), CONVERT(VARCHAR(2), DATEPART(DAY,wd)) 
    FROM 
    (
      SELECT TOP (@Weeks) DATEADD(WEEK, ROW_NUMBER() OVER 
          (ORDER BY [object_id]), @StartDate)
      FROM sys.all_objects ORDER BY [object_id]
    ) AS sq(wd)
  ),
  cte2(mn,ds) AS
  (
    SELECT DISTINCT mn, m + STUFF((SELECT ', ' + d FROM cte1 AS cte1_a 
      WHERE cte1_a.m = cte1.m ORDER BY mn
      FOR XML PATH(''), TYPE).value('.[1]','varchar(max)'),1,1,'') 
      FROM cte1
  )
  SELECT @output = STUFF((SELECT ' | ' + ds FROM cte2 ORDER BY mn
      FOR XML PATH(''),TYPE).value('.[1]','varchar(max)'),1,3,'');

  RETURN (@output);
END
GO
于 2013-08-15T01:28:32.247 回答