0

I have the following ID in my database:

Declare @YourTable Table ([SomeCol] varchar(50))
Insert Into @YourTable Values 
('1111-DXA-'),
('111-BCN-C1'),
('111-DEL-C1'),
('111JK1004JK1-000000/002'),
('11111-GOT-17C5'),
('11111-JHB-0' );


SELECT RTRIM(LTRIM(REVERSE( 
            SUBSTRING(
                    SUBSTRING(REVERSE([SomeCol]) , CHARINDEX('-', REVERSE([SomeCol]))  +1 , LEN([SomeCol]) )
                     , 1 , CHARINDEX('-', SUBSTRING(REVERSE([SomeCol]) , CHARINDEX('-', REVERSE([SomeCol]))  +1 , LEN([SomeCol]) ) ) -1
                    )
                )))
FROM @YourTable

Exception :Invalid length parameter passed to the LEFT or SUBSTRING function.

Can you pls suggest me on this.

NarasimhaKolla
  • 791
  • 3
  • 7
  • 14
  • It's because on the fourth row you only have one hyphen and that's messing up with your substring function. Do all strings have exactly two hyphens? Which part of string are you trying to capture? – Radagast Aug 12 '20 at 16:15
  • The fourth value only has a single hyphen. Is that possibly the cause? – shawnt00 Aug 12 '20 at 16:15
  • DXA,BCN,DEL,JK1,GOT,JHB ; I need to process fourth row also. There will be single hyphen and also two hyphens. Can you pls suggest any other alternative. – NarasimhaKolla Aug 12 '20 at 17:42

0 Answers0