我想在包含 LIKE 运算符的查询中使用函数 split。
SELECT * FROM Tbl_News n WHERE n.Date >= @Fromdate AND n.Date <= @Todate
AND n.Title LIKE '%' + (SELECT Item FROM dbo.SplitString(@key, ',')) + '%'
n.Title 类型为 nvarchar。 但引发此错误:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
最佳答案
尝试使用APPLY
:
SELECT n.*
FROM Tbl_News n CROSS APPLY
(SELECT Item FROM dbo.SplitString(@key, ',')) s(item)
WHERE n.Date >= @Fromdate AND
n.Date <= @Todate AND
n.Title LIKE '%' + s.item + '%' ;
如果您关心性能,您可以考虑对标题使用全文索引。
关于sql - 如何在使用 LIKE 运算符的地方使用 split 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33721924/