Need SQL function to get part of sring


using Microsoft SQL Server 2005. I need a SQLfunction or statement to get part of a varchar.

All the values will have 2 dashes '-' like this:

AZ - District of Arizona - Phoenix
CA - Central District of California - San Fernando Valley
FL - Northern District of Florida - Tallahassee

I need to get everything to the left of the second dash without the space before it.  So I would need

AZ - District of Arizona<no space>
CA - Central District of California
FL - Northern District of Florida

somewhat new I have no idea any help please
Charles BaldoSoftware DeveloperAsked:
Who is Participating?

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

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.

You don't need a special function, just some string manipulation.  You could do something like this.

SELECT SUBSTRING(YourColumn, CHARINDEX('-', YourColumn) + 2, CHARINDEX('-', YourColumn, CHARINDEX('-', YourColumn) + 1) - CHARINDEX('-', YourColumn) - 2)


Lee SavidgeCommented:
select reverse(right(reverse(mycolumn), len(mycolumn) - charindex('-', reverse(mycolumn)) - 1)) from mytable

It works. May not be the best.

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
Oops.  I thought you were trying to extract the text between the dashes.  lsavidge's solution works.  There are actually a number of ways to do it.  Here's another.

SELECT LEFT(@String, CHARINDEX('-', @String, CHARINDEX('-', @String) + 1) - 2)


Charles BaldoSoftware DeveloperAuthor Commented:
Thanks All
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
Microsoft SQL Server 2005

From novice to tech pro — start learning today.