T-SQL extract text after character and before space

If I have the following data that could look like this:
#123A4 is a code we want
a code we want is #123A4 also
a code we want is #123A4   also

How can I extract the value 123A4 in all of these cases?
maverick0728Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ManjuIT - Project ManagerCommented:
SUBSTRING(columnname, CHARINDEX('#', columnname)+1, 5)
0
NorieVBA ExpertCommented:
So it's the text between # and space?

Probably something simpler but try this.

SUBSTRING([YourField], CHARINDEX('#', [YourField]),
                 CHARINDEX(' ',[YourField]) - CHARINDEX('#', [YourField]) +1)
0
Scott PletcherSenior DBACommented:
Roughly:
SUBSTRING(column_name, CHARINDEX('#', column_name) + 1, CHARINDEX(' ', column_name + ' ', CHARINDEX('#', column_name) - CHARINDEX('#', column_name))
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

NorieVBA ExpertCommented:
Oops, misread the question completely.

If you want to get rid of the # and trailing space try this.

SELECT REPLACE(RTRIM([YourField]),'#','')
0
Scott PletcherSenior DBACommented:
RTRIM(SUBSTRING(column_name, CHARINDEX('#', column_name) + 1, CHARINDEX(' ', column_name + ' ', CHARINDEX('#', column_name) - CHARINDEX('#', column_name))))

select column_name, RTRIM(SUBSTRING(column_name, CHARINDEX('#', column_name) + 1, CHARINDEX(' ', column_name + ' ', CHARINDEX('#', column_name) - CHARINDEX('#', column_name))))
from (values('#123A4'),('#123A  ...'),('#123A4   also')) as x(column_name)
0
maverick0728Author Commented:
Scott Pletcher,

RTRIM(SUBSTRING(column_name, CHARINDEX('#', column_name) + 1, CHARINDEX(' ', column_name + ' ', CHARINDEX('#', column_name) - CHARINDEX('#', column_name))))

this works except it is grabbing the next character after the space, so it is returning:
123A4 i
123A4 a
123A4
0
NorieVBA ExpertCommented:
Perhaps.

SELECT SUBSTRING(STUFF([ColumnName],1,CHARINDEX('#',[ColumnName]),''),1,CHARINDEX(' ',STUFF([ColumnName],1,CHARINDEX('#',[ColumnName]),'')))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
Oops:

RTRIM(SUBSTRING(column_name, CHARINDEX('#', column_name) + 1, CHARINDEX(' ', column_name + ' ', CHARINDEX('#', column_name) - CHARINDEX('#', column_name)) - 1))
0
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
If the pattern fixed, i.e. will the string of interest always be 5 characters in length with the following pattern?
- 1st - 3rd places: Three (03) numbers
- 4th place: One (01) alphabet
- 5th place: A Number

If this, the following will work:

USE tempdb;
GO
DECLARE @dummyTable TABLE 
    (dummyValues VARCHAR(20));

INSERT INTO @dummyTable
    (dummyValues)
VALUES
    ('#123A4'),
    (' #123A4'),
    ('#123A4 '),
    ('5678'),    --ignore this, not in proper pattern
    ('#5678'),   --ignore this, not in proper pattern
    ('#567Z8'),
    ('#567Z8 ');

SELECT SUBSTRING(dt.dummyValues,
                 PATINDEX('%[1-9][1-9][1-9][a-zA-z][1-9]%',dt.dummyValues),
                 5
                )
FROM @dummyTable AS dt
WHERE PATINDEX('%[1-9][1-9][1-9][a-zA-z][1-9]%',dt.dummyValues) > 0
/****************
RESULTS
-------------
123A4
123A4
123A4
567Z8
567Z8
****************/

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.