传递给SUBSTRING函数的长度参数无效 - 与日期格式相关

时间:2015-02-02 21:09:38

标签: sql-server

以下查询返回以下错误: "传递给LEFT或SUBSTRING函数的长度参数无效。"

当它正确运行时,它会返回case_id以及ct.case_text列中第8个和第9个制表符之间的数据。

查询中SUBSTRING函数的参数确实计算为正整数。 b.received是DATETIME列

SELECT TOP 1000 b.case_id,
SUBSTRING(ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(
          CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, 
              CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.
                    case_text) + 1) + 1) + 1) + 1) + 1) + 1) + 1) + 1, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(
              CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, 
                  CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text) + 1) + 1) + 1) + 1) + 1) + 1) + 1
      ) + 1) - CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.
          case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), 
                ct.case_text, CHARINDEX(CHAR(9), ct.case_text, CHARINDEX(CHAR(9), ct.case_text) + 1) + 1) + 1) 
            + 1) + 1) + 1) + 1)) 
FROM case_id b
INNER JOIN case_text ct ON b.company_id = ct.company_id
  AND b.case_id = ct.case_id
  AND ct.case_text_seq = 1
WHERE NOT EXISTS (
    SELECT *
    FROM case_text ct1
    WHERE ct1.case_id = ct.case_id
      AND ct1.case_text_seq = 3
    )
  AND b.received BETWEEN CAST('2014-12-01' AS DATE) AND CAST('2015-01-29' AS DATE)
  AND b.b13_code IS NOT NULL
  AND ct.description IS NULL 
  AND b.case_id IN (4222729, 2840639, 3359716, 3365915)

如果我注释掉下面的一行,则错误消失。

AND b.received BETWEEN CAST('2014-12-01' AS DATE) AND CAST('2015-01-29' AS DATE)

有谁知道为什么评论该行会导致它正确运行?这一条件对于确定全套数据是必要的; case_id过滤器当前用于初始测试。

1 个答案:

答案 0 :(得分:2)

以下是使用字符串拆分器的示例。你可以在这里阅读更多相关信息。 http://sqlperformance.com/2012/07/t-sql-queries/split-strings

我个人更喜欢Moden分离器。它有一些缺点和局限,但如果你保持在界限范围内,它会非常快。

SELECT TOP 1000 b.case_id
    , s.Item
FROM case_id b
INNER JOIN case_text ct ON b.company_id = ct.company_id
  AND b.case_id = ct.case_id
  AND ct.case_text_seq = 1
cross apply dbo.DelimitedSplit8K(case_text, CHAR(9)) s
WHERE NOT EXISTS (
    SELECT *
    FROM case_text ct1
    WHERE ct1.case_id = ct.case_id
      AND ct1.case_text_seq = 3
    )
  AND b.received BETWEEN CAST('2014-12-01' AS DATE) AND CAST('2015-01-29' AS DATE)
  AND b.b13_code IS NOT NULL
  AND ct.description IS NULL 
  AND b.case_id IN (4222729, 2840639, 3359716, 3365915)
  AND s.ItemNumber = 8

为了完整起见,这里是DelimitedSplit8K函数的代码。

CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
        (@pString VARCHAR(8000), @pDelimiter CHAR(1))
RETURNS TABLE WITH SCHEMABINDING AS
 RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 0 up to 10,000...
     -- enough to cover VARCHAR(8000)
  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "zero base" and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT 0 UNION ALL
                 SELECT TOP (DATALENGTH(ISNULL(@pString,1))) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT t.N+1
                   FROM cteTally t
                  WHERE (SUBSTRING(@pString,t.N,1) = @pDelimiter OR t.N = 0) 
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY s.N1),
        Item       = SUBSTRING(@pString,s.N1,ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000))
   FROM cteStart s
;

最后但非常重要的评论,你有前1000名,但没有订单。如果不对结果进行排序,您将无法知道将获得哪些行。