Invalid length parameter

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

Mark WilsonBI DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
It's hard to find which part of code returning the error since we don't have your data. You'll need to check if any of the PATINDEX functions isn't returning a valid number (1 or superior):
LEFT(RIGHT([L].[LookupString], LEN([L].[LookupString]) - PATINDEX('%[[]%', [L].[LookupString])), PATINDEX('%]%', RIGHT([L].[LookupString], LEN([L].[LookupString]) - PATINDEX('%[[]%', [L].[LookupString]))) - 1)
Scott PletcherSenior DBACommented:
It seems like it must be that '[' is missing.
I'd use a CROSS APPLY to assign an alias name for the open_bracket location, then you can test for 0 and resolve that however you need to.

;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]) - open_bracket),
                          PATINDEX('%]%', RIGHT([L].[LookupString], LEN([L].[LookupString]) - open_bracket)) - 1) [CA],
                     CAST('<XL>' + REPLACE(LEFT(RIGHT([L].[LookupString], LEN([L].[LookupString]) - open_bracket),
                          PATINDEX('%]%', RIGHT([L].[LookupString], LEN([L].[LookupString]) - open_bracket)) - 1),
                          ',', '</XL><XL>') + '</XL>' AS XML) AS [C]
              FROM [Meta].[Lookup]  [L]
             
             CROSS APPLY (
                     SELECT CASE WHEN PATINDEX('%[[]%', [L].[LookupString]) = 0
                                 THEN 1 --adjust depending on how you want to handle missing '['
                                 ELSE PATINDEX('%[[]%', [L].[LookupString]) END AS open_bracket
              ) AS assign_alias_names


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

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

)

UPDATE  [Motor].Matter
...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.