Reputation: 193
I have 2 strings , let's suppose stringA and stringB . I want to know if stringA contains stringB. Since I am new to SQL Server and T SQL. I am not sure what prebuild function can I use .
It should be something like this.
If (contains(stringA,stringB))
then Print 'It Contains it'
Upvotes: 1
Views: 2711
Reputation: 193
1-
if CHARINDEX('ME','MEMOZA') > 0
begin
PRINT 'TRUE'
end
2- As suggested by Giannis Paraskevopulos
IF 'MEMOZA' LIKE '%' + 'MO' + '%'
Print 'It Contains it'
ELSE
PRINT 'It doesn''t'
Upvotes: 1
Reputation: 2477
Let me throw in the actual T-SQL CONTAINS keyword, just for good measure, IF you are going to check for column content.
Upvotes: 0
Reputation: 460340
You can use LIKE
:
SELECT t.* FROM dbo.Table t
WHERE stringA LIKE '%' + @stringB + '%'
or with an IF
:
IF @stringA LIKE '%' + @stringB + '%' PRINT 'It contains it'
ELSE PRINT 'It does not contain it';
Upvotes: 3
Reputation: 18431
DECLARE @StringA VARCHAR(100)
DECLARE @StringB VARCHAR(100)
SET @StringA = 'ABCDEF'
SET @StringB= 'CD'
IF @StringA LIKE '%' + @StringB + '%'
Print 'It Contains it'
ELSE
PRINT 'It doesn''t'
SET @StringA = 'ABCDEF'
SET @StringB= 'WU'
IF @StringA LIKE '%' + @StringB + '%'
Print 'It Contains it'
ELSE
PRINT 'It doesn''t'
Upvotes: 1