0

In SQL I have a field that has a lot of text in it.

I am trying to find a way to get text from the field if it contains certain words.

An example:

textField = 'This is the value for your spaceID=12345678'

textField = 'This is the value for your typeID=43254364'

So if the textField contains spaceID I want to return the value for it

If textField like spaceID then return 12345678

I am just not sure how to return the value of that ID.

1

3 Answers 3

1

Hi i think thoses example can help you :

declare @value varchar(400)

SET @value = 'This is the value for your spaceID=12345678'
select @value, CHARINDEX('=',@value,1), SUBSTRING(@value,CHARINDEX('=',@value,1) + 1 ,10)
where @value like '%spaceID%'

And see thoses different link :

Sign up to request clarification or add additional context in comments.

2 Comments

This script fully rely on "=", it will not return expected result if some reason there is another occurrence of "=" .
@PawelCzapski thx you . Yes it’s just example they have to adapt this with they data
1

You can do it with substring():

select substring(textfield, charindex('spaceID=', textfield) + len('spaceID='), 100)
from tablename
where textfield like '%spaceID=%'

4 Comments

this is more defensive answer, I like you used "len" instead hardcoding
i guess spaceID= is just a sample literal string, so the code should be flexible with any other string.
Yeap, still better use "spaceID=" than just "=", as this may occur in other places also.
Just to be on the safe side.
1

You can use substring() :

select substring(textField, charindex('spaceID=', textfield) + 8, len(textField))

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.