Search code examples
sql-serversql-server-2005

Searching two columns and having them treated like they're one column


I build this application that does a search for a certain column(a). Turns out the data that is going in column(a) needs to be split up into to columns. Unfortunately this makes my searching mechanism messed up. Before I would do something like this.

columnA like '%" & q & "%'

I need the search now split into two columns, but still treated as if the data still is in one column (concatenated).

columnA+columnB LIKE '%" & q & "%'

Is this possible?


Solution

  • Yes, if I understand your question:

    SELECT 
            CONVERT(nvarchar(1024),(ISNULL(columnA,''))+CONVERT(nvarchar(1024),ISNULL(columnB,''))  
    FROM searchTable
    WHERE 
        CONVERT(nvarchar(1024),(ISNULL(columnA,''))+CONVERT(nvarchar(1024),ISNULL(columnB,'')) LIKE '%" & q & "%'
    

    Is that what you are looking for?