【问题标题】:Collect values between two words - sql收集两个单词之间的值 - sql
【发布时间】:2019-01-05 23:10:59
【问题描述】:

我一直致力于收集两个特定单词之间的值。

我有一个工作正常,但另一个似乎抛出了一个我无法解决的错误......

字段值(NVARCHAR):

** Service Details **  Service Line: 0000000010  Service No: 000000000003001441  Service Text: SMPR Small meter problem  Service Qty: 55.06    ** Appointment Details **  17.07.2018 11:55 To 16.08.2018 11:55  

我正在使用的脚本:

REPLACE(SUBSTRING(SUBSTRING([description_long],CHARINDEX('Service No:',[description_long])-1,CHARINDEX('Service Text:',[description_long])-CHARINDEX('Service No:',[description_long])+0), 14, 100),char(13),'') AS [serviceNo] 
REPLACE(SUBSTRING(SUBSTRING([description_long],CHARINDEX('Service Text:',[description_long])-1,CHARINDEX('Service Qty:',[description_long])-CHARINDEX('Service Text:',[description_long])+0), 16, 2000),char(13),'') AS [serviceText]

所以服务行给了我:0000000010,但服务文本给了我:

传递给 LEFT 或 SUBSTRING 函数的长度参数无效。

我试过把“-”改成“+”

REPLACE(SUBSTRING(SUBSTRING([description_long],CHARINDEX('Service Text:',[description_long])-1,CHARINDEX('Service Qty:',[description_long])+CHARINDEX('Service Text:',[description_long])+0), 16, 2000),char(13),'') AS [serviceText]

这没有给我错误和价值:

 SMPR Small meter problem  Service Qty: 55.06    ** Appointment Details**  17.07.2018 11:55 To 16.08.2018 11:55

我哪里错了? - 任何建议表示赞赏。

【问题讨论】:

    标签: sql sql-server substring charindex


    【解决方案1】:

    这样的?

    DECLARE @T TABLE (Description_long nvarchar(1000));
    
    INSERT INTO @T VALUES (N'** Service Details **  Service Line: 0000000010  Service No: 000000000003001441  Service Text: SMPR Small meter problem  Service Qty: 55.06    ** Appointment Details **  17.07.2018 11:55 To 16.08.2018 11:55  ');
    
    SELECT 
     RTRIM(
      REPLACE(
       SUBSTRING(
        [description_long],
        CHARINDEX('Service No:', [description_long]),
        CHARINDEX('Service Text:',[description_long]) - CHARINDEX('Service No:', [description_long])
      ), 
      'Service No: ', '')) AS [serviceNo],
    
     RTRIM(
      REPLACE(
       SUBSTRING(
        [description_long],
        CHARINDEX('Service Text:', [description_long]),
        CHARINDEX('Service Qty:',[description_long]) - CHARINDEX('Service Text:', [description_long])
       ),
      'Service Text: ', '')) AS [serviceText]
    
    FROM @T
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 2019-03-01
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多