return data to left of dash excluding the dash

I have a database of inventory items that have prefixes based on manufacturers.  We want to compile a list of these prefixes.  I need to return only the data to the left of the first dash in the ItemNumber.  There could be items with no dash.  Any help you can provide is much appreciated.  Thanks,
LVL 2
mike247Asked:
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.

Mark WillsTopic AdvisorCommented:
Something like :
Select itemNumber, left(itemnumber,charindex('-',itemnumber) -1) as manufacturer
from yourtable

Open in new window


Some samples might make it easier :)
0
Mark WillsTopic AdvisorCommented:
Oh and forgot to check for length first, or, can replace '-'

Select itemNumber, replace(left(itemnumber,charindex('-',itemnumber) ),'-','') as manufacturer
from yourtable

Open in new window

Or check for '-' using IIF() which was introduced in SQL2012
Select itemNumber, iif(charindex('-',itemnumber)>1,left(itemnumber,charindex('-',itemnumber) -1),'') as manufacturer
from yourtable

Open in new window

pre sql2012 you would need to use a CASE instead
Select itemNumber, case when charindex('-',itemnumber)>1 then left(itemnumber,charindex('-',itemnumber) -1) else '' end as manufacturer
from yourtable

Open in new window

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:
SELECT LEFT(ItemNumber, CHARINDEX('-', ItemNumber + '-') - 1) AS TrimmedItemNumber

FROM ...
1
Mark WillsTopic AdvisorCommented:
G'day Scott,

Yeah, was thinking of that except it would return the entire itemnumber when all we need is the Manufacturer code - which may not exist. So, thought it best not to return the entire itemnumber where there is no manufacturer code / prefix...

Seem to recall a similar question about prefixes but in a trigger from Mike...

Just a thought......

Cheers,
Mark
0
mike247Author Commented:
Sorry, I thought I had closed this out the day the answer came in.  I guess I didn't click through all the "next" buttons.
0
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
Databases

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.