SQL Server: How to Check If a string Contains A Specific Letters / Characters

We could use CHARINDEX for checking if a specific letter / character / substring is exist in a text of string.

Below sample is how to use it:

DECLARE @MyString NVARCHAR(100)= 'Gedung Merdeka Ada di Jalan Asia Afrika'  
IF CHARINDEX('Jalan',@MyString) > 0   
BEGIN
   SELECT 'Found' As Result  
END
ELSE
   SELECT 'Not Found' As Result  


DECLARE @MyString NVARCHAR(100)= 'Gedung Merdeka Ada di Jalan Asia Afrika'  
IF CHARINDEX('Mobil',@MyString) > 0   
BEGIN
   SELECT 'Found' As Result  
END
ELSE
   SELECT 'Not Found' As Result  


I usually use this on IF and CASE WHEN condition in SQL Query, becase this function is only to check it contains or not.  

SELECT FROM item WHERE itemname LIKE '%SAMBAL%'




Post a Comment

0 Comments