Search by like on sql server 2005

Unanswered Search by like on sql server 2005

  • 23 мая 2012 г. 11:46
     
      С кодом

    Hi, I am doing a search by like on sql server 2005 with

    select * from tableA
    where ColumnB like 'Key1'
    or ColumnB like 'Key2'

    How can I keep track of the results so that I can know which key is hit on the search?

Все ответы

  • 24 мая 2012 г. 4:34
    Модератор
     
      С кодом

    Hi CMF0923,

    If you are trying to return rows with the value of 'ColumnB' in 'Key1' and 'Key2', you can use '=' instead of 'like'. In this case, the result of two columns will indicate the match conditions:

    select * from tableAwhere ColumnB ='Key1' or ColumnB ='Key2'


    Stephanie Lv

    TechNet Community Support

  • 24 мая 2012 г. 8:24
     
      С кодом

    HI, I need to search the Key from the content, which may not be an exact match. Below is my edited query.

    select * from tableA
    where ColumnB like '%Key1%'
    or ColumnB like '%Key2%'

  • 4 июня 2012 г. 8:23
    Модератор
     
      С кодом

    CMF0923,

    In this case, you may try to use UNION ALL to do this:

    select *,'Key1' as flag from tableA where ColumnB like '%Key1%'
    union all 
    select *,*,'Key2' as flag from tableA where ColumnB like '%Key2%'

    Stephanie Lv

    TechNet Community Support