Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

SQL split field entry only returning first entry before a comma

Avatar of tobzzz
tobzzzFlag for Spain asked on
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008
7 Comments1 Solution549 ViewsLast Modified:
I'm using MS SQL Server 2008, I have a field called townName that could contain data like:
Lincoln
or like:
Lincoln,Boston,York

I have an inner join between two tables and basically I want the join to be based on the first part of the field, before the first comma.

My SQL at the moment is:
SELECT TWN.id
FROM town TWN
    INNER JOIN otherTown OTH ON TWN.townName= OTH.townName
WHERE TWN.active = 1

but it's not getting a result because it's effectively saying:
INNER JOIN otherTown OTH ON 'Lincoln' = 'LIncoln,Boston,York'

So how do I split OTH.townName to the bit before the first comma only and rememebering that it might not contain a comma if there's only one town entered into the otherTown field.

I hope that makes sense :-)

Thanks experts!
ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmadaFlag of Canada image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answers