Click here to Skip to main content
15,909,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
create procedure getdata
@subcomponent varchar(50)
as
begin

declare @var varchar(50) =@subcompname
;WITH CTE AS (
    SELECT Sno, Comp, SubComp,
    LEFT(FromValue, PATINDEX('%[0-9]%', FromValue)-1) As FromLetter,
    CAST(RIGHT(FromValue, LEN(FromValue) - (PATINDEX('%[0-9]%', FromValue)-1)) as int) As FromNumber,
    LEFT(ToValue, PATINDEX('%[0-9]', ToValue)-1) As ToLetter,
    CAST(RIGHT(ToValue, LEN(ToValue) - (PATINDEX('%[0-9]%', ToValue)-1)) as int) As ToNumber
    FROM
    (
    SELECT Sno, Comp, SubComp,
       LEFT(SubComp,
          CASE WHEN CHARINDEX(' to ', SubComp) >; 0 THEN
            CHARINDEX('to ', SubComp)-1
          WHEN CHARINDEX(',', SubComp) >; 0 THEN
            CHARINDEX(',', SubComp)-1
          END
       ) FromValue,
       RIGHT(SubComp,
          CASE WHEN CHARINDEX(' to ', SubComp) > 0 THEN
            LEN(SubComp) - (CHARINDEX(' to ', SubComp) + 3)
          WHEN CHARINDEX(',', SubComp) > 0 THEN
            CHARINDEX(',', SubComp)-1
          END
       ) ToValue
    FROM T
    ) InnerQuery
 )

 SELECT Sno, Comp, SubComp
 FROM CTE
 WHERE LEFT(@Var, PATINDEX('%[0-9]%', @Var)-1) BETWEEN FromLetter AND ToLetter
 AND CAST(RIGHT(@Var, LEN(@Var) - (PATINDEX('%[0-9]%', @Var)-1)) as int) BETWEEN FromNumber And ToNumber

this is my procedure ............

I am getting error ....
Quote:
"invalid length parameter passed to the left or substring function in sql server"

please help me...
Posted
Updated 5-May-15 2:34am
v3
Comments
CHill60 5-May-15 8:38am    
If @Var does not contain a numeric value (or is null) then you are trying to check for LEFT(@Var, -1) BETWEEN FromLetter and ToLetter. The same is true for FromValue and ToValue.
Check your data matches what you are expecting and handle NULL values.
vemsoft 5-May-15 8:54am    
please help me with query...iam new to procedures

I cant really answer but I can tell you that it has to do with the -1 in the function, here is an article that im sure could really help you

http://www.sql-server-helper.com/error-messages/msg-536.aspx[^]

try it out, maybe it holds your answer
 
Share this answer
 
This error is related to "LEFT" function used in your SP, the issue probably may be that the second parameter value is not returing a positive integer (Please Note : PATINDEX returns NULL value as well)

These links might help you further :
https://msdn.microsoft.com/en-IN/library/ms177601.aspx[^]
https://msdn.microsoft.com/en-us/library/ms188395.aspx[^]
 
Share this answer
 
Comments
vemsoft 5-May-15 8:54am    
please help me with query...........
ConnectingKamlesh 5-May-15 9:49am    
First step : try removing -1 from "Left" function
i.e. instead of :
LEFT(FromValue, PATINDEX('%[0-9]%', FromValue)-1)
try this
LEFT(FromValue, PATINDEX('%[0-9]%', FromValue))


If this does not help

Try putting ISNULL check in "Left" function
i.e. LEFT(FromValue, ISNULL(PATINDEX('%[0-9]%', FromValue),0))

do the same for all the "Left" functions used in your SP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900