PriceCheaperton
PriceCheaperton

Reputation: 5349

Return section of a string

Lets say i have a value like this:

Z00056ABC00044

I would like to know what the values are BEFORE ABC So my query should return only:

Z00056

I have tried to create a function:

if CHARINDEX('ABC',@mainString) > 0
begin
    --do something
end

But i am unsure how to grab the value before ABC

Upvotes: 1

Views: 66

Answers (3)

Onur Cete
Onur Cete

Reputation: 273

This can be useful for you ;

select  SUBSTR('Z00056ABC00044', 1, INSTR('Z00056ABC00044', 'ABC')-1) from dual

Output :

Z00056

Upvotes: 0

rory.ap
rory.ap

Reputation: 35290

Try this:

SELECT LEFT(@MyValue, (LEN(@MyValue) - CHARINDEX('ABC', @MyValue) - 1))

Upvotes: 0

Ankit Bajpai
Ankit Bajpai

Reputation: 13509

Try this:-

SELECT SUBSTRING('Z00056ABC00044', 1, CHARINDEX('ABC', 'Z00056ABC00044') -1)

Upvotes: 2

Related Questions