sql check if string contains letters – How To Check If A String Contains A Substring In SQL Server?

sql check if string contains letters – Method 1 – Using CHARINDEX() function, Method 2 – Using LIKE Predicate – To check if string contains letters uses the operator LIKE with the following regular expression ‘[A-Za-z]%’.

sql check if string contains letters

Check if string contains letters : How To Check If A String Contains A Substring In SQL Server?

check if string contains substring sql

Declare @mainString nvarchar(100)='Pakainfo Website Examples'  
---Check here @mainString contains Pakainfo or not, if it contains then retrun greater than 0 then print detect otherwise Not detect  
if CHARINDEX('Pakainfo',@mainString) > 0   
begin  
   select 'detect' As Result  
end  
else  
    select 'Not detect' As Result 

sql where contains part of string

To detect an exact string

SELECT * FROM [table] WHERE [field] LIKE '%stringtosearchfor%'.

Method 2 – Using LIKE Predicate

DECLARE @WholeString VARCHAR(50)
DECLARE  @ExpressionTodetect VARCHAR(50)
SET @WholeString = 'Pakainfo Website Examples'
SET @ExpressionTodetect = 'Website'

IF @WholeString LIKE '%' + @ExpressionTodetect + '%'
    PRINT 'Yes it is detect'
ELSE
    PRINT 'It doesn''t detect'

The following SELECT satement selects records from Members table of Northwind database where Member’s Title contains a substring, ‘Sales’.

Read Also:  Laravel Update Multiple Records using Eloquent

SELECT [MemberID]
,[LastName]
,[FirstName]
,[Title]
FROM [NORTHWND].[dbo].[Members]
WHERE Title LIKE '%Sales%'

SQL Check if string contains letters

Return all rows

DECLARE @ShopProducts table (name varchar(250));
INSERT INTO @ShopProducts(name) values ('abcABCdef');
INSERT INTO @ShopProducts(name) values ('111');
INSERT INTO @ShopProducts(name) values ('ab5cABCdef2');
SELECT * FROM @ShopProducts;
GO

Return rows that contains letters

DECLARE @ShopProducts table (name varchar(250));
INSERT INTO @ShopProducts(name) values ('abcABCdef');
INSERT INTO @ShopProducts(name) values ('111');
INSERT INTO @ShopProducts(name) values ('ab5cABCdef2');
SELECT * FROM @ShopProducts WHERE name LIKE '[A-Za-z]%';
GO

Don’t Miss : PHP Preg_match

I hope you get an idea about sql check if string contains letters.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.