We help IT Professionals succeed at work.
Get Started

Invalid length parameter

75 Views
Last Modified: 2016-05-06
I have the following query I am using - it throws up the following error message

Invalid length parameter passed to the LEFT or SUBSTRING function.
The statement has been terminated.

Can anybody help?



;WITH [WL] AS (

SELECT        [L].[LookupId],
       [L].[LookupRuleSet],
       [L].[LookupVariety], 
       [L].[LookupType],
       [L].[LookupValue],
        REPLACE([L].[LookupString], '[' + [SV].[CA] + ']', '') [LookupString],
        [SV].[C] [CaseCategory]

FROM [Meta].[Lookup] [L]
LEFT OUTER JOIN (

       SELECT [CS].[LookupId],
              [CS].[CA],
              [SPLIT].[CS].value('.', 'INT') AS [C]

       FROM (

              SELECT [L].[LookupId],
                     LEFT(RIGHT([L].[LookupString], LEN([L].[LookupString]) - PATINDEX('%[[]%', [L].[LookupString])), PATINDEX('%]%', RIGHT([L].[LookupString], LEN([L].[LookupString]) - PATINDEX('%[[]%', [L].[LookupString]))) - 1) [CA],
                     CAST('<XL>' + REPLACE(LEFT(RIGHT([L].[LookupString], LEN([L].[LookupString]) - PATINDEX('%[[]%', [L].[LookupString])), PATINDEX('%]%', RIGHT([L].[LookupString], LEN([L].[LookupString]) - PATINDEX('%[[]%', [L].[LookupString]))) - 1), ',', '</XL><XL>') + '</XL>' AS XML) AS [C] 

              FROM [Meta].[Lookup]  [L]

       ) AS [CS] CROSS APPLY [C].nodes ('/XL') AS SPLIT([CS])

) AS [SV] ON [L].[LookupId] = [SV].[LookupId]

)



UPDATE  [Motor].Matter
SET     WorkSource = CASE WHEN [WL].[LookupValue] IS NULL THEN 'Others'
                          WHEN [Matter].[System] = 'IMELEgis' THEN 'IMe'
                          ELSE [WL].[LookupValue]
                     END
FROM    [Motor].[Matter] [Matter]
        LEFT OUTER JOIN [Motor].[Client] cl ON cl.ClaimID = Matter.ClaimID
        LEFT OUTER JOIN [WL] ON [Matter].[CaseType]
                                + COALESCE([Matter].[NUScheme], '') LIKE CASE
                                                              WHEN [WL].[LookupType] IN (
                                                              'Endswith',
                                                              'Wildcard' )
                                                              THEN '%'
                                                              ELSE ''
                                                              END
                                + REPLACE([WL].[LookupString], '||', '%')
                                + CASE WHEN [WL].[LookupType] IN (
                                            'Startswith', 'Wildcard' )
                                       THEN '%'
                                       ELSE ''
                                  END
                                AND [cl].[MotorCostLienCat2] = [WL].[CaseCategory]
                                AND [WL].[LookupRuleSet] = 'Motor'
                                AND [WL].[LookupVariety] = 'Worksource'

Open in new window

Comment
Watch Question
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE