SQL string manipulation [Get all text left of '(']

后端 未结 4 793
北恋
北恋 2021-01-02 09:08

I have some data which looks like so:

SourceOfBooking
----------------
Company1 (Foo)
Company2 (Bar)
Company3 (Foo1)
Company4 (Foo2)

I am l

相关标签:
4条回答
  • 2021-01-02 09:29

    I think you've just put a wrong character

    case
        when CHARINDEX('(', SourceOfBooking) > 0 then
            rtrim(left(SourceOfBooking, CHARINDEX('(', SourceOfBooking) - 1))
        else
            SourceOfBooking
    end
    
    0 讨论(0)
  • 2021-01-02 09:32

    You can;

    LEFT(SourceOfBooking, CHARINDEX(' (', SourceOfBooking + ' (') - 1)
    

    (Remove + ' (' if needed; it allows for rows with no  (

    0 讨论(0)
  • 2021-01-02 09:37

    This will return the company name whether or not there is a bracket, and will also handle cases where there is no space before the bracket:

    select case 
        when CHARINDEX('(', SourceOfBooking) > 0
        then RTRIM(LEFT(SourceOfBooking, CHARINDEX('(', SourceOfBooking) - 1))
        else SourceOfBooking
    end
    from Table1
    

    SQL Fiddle Example

    0 讨论(0)
  • 2021-01-02 09:53

    You cam simply try this.

    Create a new table and insert the data as below scripts. Then execute the select query. You will get required output.

    create table teststring
    (name varchar(20))
    
    insert  into teststring values ('ashish (123)')
    insert  into teststring values ('ashish jain (123)')
    
    
    select substring(name,1,charindex('(',name)-1)abc ,name from teststring
    
    0 讨论(0)
提交回复
热议问题