Bhaskar
Bhaskar

Reputation: 35

SQL Query like Condition not working as expected

I have a query in SQL Server and have values like that is enclosed in the quote. I am trying to filter the value which has CC_ somewhere in the string. In the query when I try to filter the values using %CC_%, it is still returning the values though it is not present in the string.

with a as (
Select '@IDESC("Account"),@IDESC("Period"),@IDESC("View"),@IDESC("Scenario"),@IDESC("Version"),@IDESC("Years"),@IDESC("Currency"),@IDESC("Product"),@IDESC("FX View"),@IDESC("Data_Type"),@IDESC("Entity"),@IDESC("Function"),@IDESC("Market"),@IDESC("Business_Unit"),@IDESC("Reporting_Unit")'
as val)
select * from a where val like '%CC_%'

Can experts please help?

Upvotes: 0

Views: 496

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522712

To match literal underscore in a SQL Server LIKE expression, you may place it into square brackets:

SELECT * FROM a WHERE val LIKE '%CC[_]%';

Underscore _ in a LIKE expression literally means any single character, and % means zero or more characters.

Upvotes: 2

Related Questions