Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I return all of the text to the left of a comma? 2

Status
Not open for further replies.

Mack2

Instructor
Mar 12, 2003
336
US
Hi everyone,

I need a string manipulation function to return all of the text to the left of a comma. For example..

City, Large: Territory
Suburb, Midsize:

With the two records above, I would like to return "City" for the first record.
For the second record, I only want to return suburb.

Thank you for you help!!!!


 
SELECT LEFT(YourColumnName, CHARINDEX(',', YourColumnName, 1) - 1)
 
Better yet....

Code:
SELECT LEFT(YourColumnName, CHARINDEX(',', YourColumnName [!]+ ','[/!],  1) - 1)

This protects you in case there is no comma in the data.

Ex:

Code:
Declare @Test table(YourColumnName VarChar(100))

Insert into @Test Values('City, Large: Territory')
Insert into @Test Values('Suburb, Midsize:')
Insert into @Test Values('No Comma')

SELECT LEFT(YourColumnName, CHARINDEX(',', YourColumnName + ',',  1) - 1)
From @Test


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top