Trim after special character

  • I have a dirty data, go figure right?

    I have a doctors name that is the last name / comment.  I need to trim off the / comment, last name is field, I know that this is an easy one, any suggestions?

     

    Richard

  • SELECTLEFT(value, CHARINDEX('/', value) - 1)

    FROM table

    That should do it...

  • It works perfectly!  THANK YOU!

  • Note: This won't work if any of the values does NOT contain a '/'.

    In this case charindex(value,'/')-1 evaluates to -1 causing LEFT to blow up with an invalid length error.

    You'll need to add a case something like:

    select left(value,
        case charindex(value,'/')
        when 0 then
           len(value)
        else 
           charindex(value,'/')-1
        end)
    from table 

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply