在非字母字符串中查找字符串
问题描述:
我必须在表格中找到所有字母字符串的出现。要做到这一点,我使用下面的算法(我遍历另一个表中环得到这个@tablename值):在非字母字符串中查找字符串
UPPER(rows_value) like '%' + @TableName + '%')
这种情况是错误的,因为它也显示我包含在另一个串串。
我们假设@TableName = test。我想查找表中的记录,其中包含此字符串(也被非字母字符投降)。我的算法返回行包含:
test
(test)
test0x
test02
_test_2222
pretest <---
uptest <----
...
我不需要最后两个,因为这些是不同的单词。如何修改我的条件以排除不需要的结果?
答
尝试下一个查询:
DECLARE @TableName VARCHAR(128) = 'test' -- Replace with propper data type and max length
SELECT *
FROM (VALUES
('test'),
('(test)'),
('test0x'),
('test02'),
('_test_2222'),
('pretest '),
('uptest'),
('testAlpha'),
('13223 tes3432')
) t(Col1)
WHERE t.Col1 LIKE '%' + @TableName + '%'
AND NOT(t.Col1 LIKE '%[a-z]' + @TableName + '%' OR t.Col1 LIKE '%' + @TableName + '[a-z]%')
您已经发布了你的算法的结果,但怎么样你预期的结果 – StackUser
我不需要过去两年(箭头标记)的字符串。 – PNPTestovir