user3513237
user3513237

Reputation: 1095

How do I parse out data out of a field before a slash

In MS-SQL 2008 I have a varchar field that holds data. It will have names like this:

  1. John/Cindy
  2. Steve
  3. Jack/Joe

I need the data to read in my output like this (so gathering the first name listed only if there are multiple separated by a slash):

  1. John
  2. Steve
  3. Jack

I am assuming this needs to be some type of function but not sure. Appreciate the help, thank you.

Upvotes: 0

Views: 74

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270001

SQL Server doesn't have very good string manipulation functions, but this isn't so hard:

select (case when names like '%/%'
             then left(names, charindex('/', names) - 1)
             else names
        end)

EDIT:

Mikael's suggestion saves the case statement:

select left(names + '/', charindex('/', names) - 1)

Upvotes: 2

Related Questions