title: Error : Invalid length parameter passed to the LEFT or SUBSTRING function
body: <p>My Function is as Following :: </p>
  
  <hr>
  
  <pre><code>Create Function dbo.fnFcid(@url varchar)
   Returns int
  
   As
  
   BEGIN
   Return ( select ID from  TTI_fat.dbo.FormContext
            where caseId = (SELECT SUBSTRING(@url, PATINDEX('%&amp;c=%', @url)+ 3, (PATINDEX('%&amp;y=%', @url) - (PATINDEX('%&amp;c=%', @url)+3)))) 
            and   YearID = (SELECT SUBSTRING(@url, PATINDEX('%&amp;y=%', @url)+ 3, (PATINDEX('%&amp;e=%', @url) - (PATINDEX('%&amp;y=%', @url)+3)))) 
            and   EntityID = (SELECT SUBSTRING(@url, PATINDEX('%&amp;e=%', @url)+ 3, (PATINDEX('%&amp;g=%', @url) - (PATINDEX('%&amp;e=%', @url)+3))))
            and   GroupID = (SELECT SUBSTRING(@url, PATINDEX('%&amp;g=%', @url)+ 3, (PATINDEX('%&amp;v=%', @url) - (PATINDEX('%&amp;g=%', @url)+3)))) 
           )  
     END;
   Go
  </code></pre>
  
  <hr>
  
  <p>when I am passing a following value to function  </p>
  
  <hr>
  
  <pre><code>select dbo.fnFcid('http://localh0st/web/forms/getForm.asp?page=FKYASDENPLFPg1V2014&amp;c=9&amp;y=283&amp;e=1022&amp;g=2&amp;v=1&amp;x=296807&amp;j=161037&amp;z=1')
  </code></pre>
  
  <hr>
  
  <p>its shows me following error </p>
  
  <hr>
  
  <pre><code>Msg 537, Level 16, State 2, Line 1
  Invalid length parameter passed to the LEFT or SUBSTRING function.
  </code></pre>