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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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)
NorieAnalyst Assistant Commented:
So it's the text between # and space?

Probably something simpler but try this.

SUBSTRING([YourField], CHARINDEX('#', [YourField]),
                 CHARINDEX(' ',[YourField]) - CHARINDEX('#', [YourField]) +1)
Scott PletcherSenior DBACommented:
Roughly:
SUBSTRING(column_name, CHARINDEX('#', column_name) + 1, CHARINDEX(' ', column_name + ' ', CHARINDEX('#', column_name) - CHARINDEX('#', column_name))
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

NorieAnalyst Assistant Commented:
Oops, misread the question completely.

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

SELECT REPLACE(RTRIM([YourField]),'#','')
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)
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
NorieAnalyst Assistant Commented:
Perhaps.

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

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))
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

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.