sql server - Error in the query "Invalid length parameter passed to the LEFT or SUBSTRING function." -


enter image description hereenter image description herehello in below query facing issues of "invalid length parameter passed left or substring function." can please solve error me can work on query.

select   substring(     a.processinstancedescription,      charindex('http://', a.processinstancedescription ),      charindex('keyinstr', a.processinstancedescription )        - charindex('http://', a.processinstancedescription )),    c.streamname category,   processinstanceappianid jobid,   a.processinstancename,   a.processinstancetargetdate targetdate,   a.processinstancedescription taskdescription,   b.name department,   substring(     processinstancename,      nullif(       patindex('%[0-9][0-9][0-9][0-9][0-9]%',processinstancename),       0),     7) code    internaluseonly..processinstance    join internaluseonly..departments b      on b.keydepartment = a.keydepartmententered      , b.updoperation < 2   join internaluseonly..processstream c      on c.keyprocessstream = a.keyprocessstream      , c.updoperation < 2    processinstancecompleted null   , a.keyprocessstream in (330)    , a.updoperation < 2 

i have attached image above. want fetch urls task description. can correct query or give me exact query perform same

length parameter substring parameter #3. when parameter <0 error occurs. means have problems first occurence of substring, particulary expression:

charindex('keyinstr', a.processinstancedescription )    - charindex('http://', a.processinstancedescription ) 

probably can't find keyinstr in processinstancedescription field , why fails.

how can guard against error depends on want do.

edit

one way deal check if token has been found ('keyinstr'). if not, substitute result desired value, 0, -1, null or else need signal unexpected situation.

case    when charindex('keyinstr', a.processinstancedescription) != 0     charindex('keyinstr', a.processinstancedescription )        - charindex('http://', a.processinstancedescription )   else 0 end 

...or in case better do...

select   case      when charindex('keyinstr', a.processinstancedescription) != 0       substring(         a.processinstancedescription,          charindex('http://', a.processinstancedescription ),          charindex('keyinstr', a.processinstancedescription )            - charindex('http://', a.processinstancedescription ))     else 0 end,    ... 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -