如何在SQL中检查字符串A是否“包含”B?

时间:2014-09-12 08:41:07

标签: sql sql-server tsql

我有两个字符串,我们假设stringA和stringB。我想知道stringA是否包含stringB。因为我是SQL Server和T SQL的新手。我不确定我可以使用什么预制功能。

它应该是这样的。

If (contains(stringA,stringB))

然后打印'It Contains it'

4 个答案:

答案 0 :(得分:2)

您可以使用LIKE

SELECT t.* FROM dbo.Table t
WHERE stringA LIKE '%' + @stringB + '%'

IF

IF @stringA LIKE '%' + @stringB + '%' PRINT 'It contains it'
ELSE PRINT 'It does not contain it';

答案 1 :(得分:1)

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'

答案 2 :(得分:1)

1 -

if CHARINDEX('ME','MEMOZA') > 0
begin
    PRINT 'TRUE'
end

2-正如Giannis Paraskevopulos所建议的

IF 'MEMOZA' LIKE '%' + 'MO' + '%' 
    Print 'It Contains it'
ELSE
    PRINT 'It doesn''t'

答案 3 :(得分:0)

如果您要检查列内容,请让我输入实际的T-SQL CONTAINS关键字,只是为了更好的衡量标准。