假设我有两列:
我希望将记录与'Silverado 1500'的字符串相匹配
我该怎么做?
我正在运行SQL Server 2014
谢谢
答案 0 :(得分:1)
您可以尝试这样的事情:
SELECT * FROM MyTable
WHERE Col1 + ' ' + Col2 LIKE '%Silverado 1500%'
答案 1 :(得分:1)
不完全是你所要求的,但这是我期望的类似搜索行为。它会为您提供多个术语的匹配结果以及匹配的术语数。
DECLARE @SearchTerms TABLE(
Term VARCHAR(200)
)
DECLARE @Table TABLE (
Col1 VARCHAR(200),
Col2 VARCHAR(200)
)
INSERT INTO @Table SELECT 'Silverado','1500 Vortec Max Crew Cab'
INSERT INTO @Table SELECT 'Honda','Accord'
DECLARE @xml as xml,@str as varchar(100),@delimiter as varchar(10)
SET @str='Silverado 1500 Crew'
SET @delimiter =' '
SET @xml = cast(('<X>'+replace(@str,@delimiter ,'</X><X>')+'</X>') as xml)
INSERT INTO @SearchTerms
SELECT N.value('.', 'varchar(10)') as value FROM @xml.nodes('X') as T(N)
SELECT t.Col1,t.Col2,count(*) [Matching Terms]
FROM @Table t
INNER JOIN @SearchTerms st on t.Col1 like '%' + st.Term + '%' OR t.Col2 like '%' + st.Term + '%'
GROUP BY t.Col1,t.Col2
ORDER BY count(*) DESC