Search code examples
sqlsql-servert-sqlsql-like

Incorrect result returned by SQL Query


The below query is returning all data instead of showing the records which consists [3428] in ApprovedByNewTExt column.

select * from 
    (
        Select Rec_Num, 
            COALESCE(AppByENo6,AppByENo5,AppByENo4,AppByENo3,AppByENo2,AppByENo1)
            As 'ApprovedByNewTExt' 
        FROM MyTable
    ) 
as output
where ApprovedByNewTExt like '%[3428]%'

Spent more time in identifying the issue but failed to fix.

Any suggestions is greatly appreciated.


Solution

  • Depends on what you're searching for.

    Right now you search for any row that contains a 3, 4, 2 or 8

    If you want to search for "3428" then drop the [ and ]

    If you want to search for "[3428]" then you need to escape the [ and ], see this SO question