2

I have a number of rows in a database column that look like this

AB01-52
AB01-52/1
AB01-53/2
AB01-54

I can get the 52 where there is no slash in the string but when I try to get it between the dash and the slash I either get an error or get the 52/1 for example

I have tried most of the researched solutions using substring and charindex but to no avail

Has anyone seen a solution for this.

Thanks in advance

Rick

8
  • Show the code you wrote that produces the wrong result. Commented May 24, 2018 at 13:25
  • case when charindex('/'field1) = 0 then substring(field1,charindex('-',field1)+1 ,len(field1)) else charindex('/'field1) = 0 then substring(field1,charindex('-',field1)+1 ,len(field1)) else SUBSTRING(fiel1,CHARINDEX('-',field1)+1,CHARINDEX('/',field1)-CHARINDEX('-',field1)-1) Commented May 24, 2018 at 13:28
  • I also am unsure how your solutions with charindex/substring were to no avail. That is absolutely how you would solve your problem. Commented May 24, 2018 at 13:29
  • 1
    If all the values you wish to extract are the 6th and 7th characters in the string, this is easy to do. Are there more variations that need to be taken into account? Commented May 24, 2018 at 13:31
  • Nope no variations there is either a slash followed by a number or there is no slash Commented May 24, 2018 at 13:33

4 Answers 4

1

You can subtract the characters positions (i.e. /) and use as length for substring()

substring(col, charindex('-', col)+1, len(col)-charindex('/', col)+1)

If the numbers have variable length then do the subtraction from both characters and use of length

substring(col, charindex('-', col)+1, charindex('/', col+'/')-charindex('-', col)-1)
Sign up to request clarification or add additional context in comments.

Comments

0

Here's one approach....

with cte as(
select 'AB01-53/2' as C1
union
select 'AB01-54')

select left(right(c1,len(c1) - charindex('-',c1)),len(right(c1,len(c1) - charindex('-',c1))) - charindex('/',right(c1,len(c1) - charindex('-',c1)))+ 1)
from cte

Comments

0

If the field is ALWAYS AAAA-BB[/CCC], then simply:

  SUNSTRING('AB01-52/1',6,2) ;

Of course, the 'AB01-52/1' may be substituted by a variable or column name.

1 Comment

In that case, update your example so people (and me) can better understand what you need.
0

I may have found an answer but it involves a 2 stage process

First add an additional slash to the column that contains the data where there is no slash at the moment ie AB01-52 becomes AB01-52/ the others like AB01-1023/01 remain the same

stage 2 use this code:

substring(right(col1,len(col1)-charindex('-',col1)),0,charindex('/',right(col1,len(col1)-charindex('-',col1))))

this allows me to get the middle string. The use of left and right by various people who contributed helped to get to this - however I still thing there should be an answer without stage 1

Thanks you all for your ideas which got me to this point

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.