Search code examples
sqlsql-serverwildcardsql-likestartswith

Filter in select where values start with NIR_


I am trying to filter my result set to only return values which start with NIR_.

My SQL statement to do so is as follows

select * from run where name like %NIR_% 

The result set also includes names like

NIRMeta_Invalid
NIRMeta_Position 

I am not sure what I am doing wrong. I only need to select names which start with NIR_.


Solution

  • You need to escape the underscore in your LIKE pattern if you want it to be treated as a literal.

    In SQL Server:

    select * from run where name like 'NIR[_]%'
    

    In MySQL and Oracle:

    select * from run where name like 'NIR\_%'