2016-04-29 12 views
1

我有一個表Document,其中有一些列,例如, DocumentTitle。看起來是這樣的:查詢有類似字符串的列表


文件

  • ID(唯一標識符)
  • DOCUMENTTITLE(VARCHAR(200))

在我的應用程序,你輸入一個字符串

你搜索像「發票4711」。現在我的申請應該搜索所有這些文字都在標題中的文件。 因此找不到標題爲foo4711.pdfinvoice_bar.pdf的文檔。 必須找到標題爲invoce 4711.pdftest4711invoce.png的文件。

因爲我在一個過程中寫這個我不能設置搜索字符串的修復參數計數。我收到一個參數,例如invoice 4711和我分方法返回一個像所有的項目表(是的,與%-signs)


項目

%4711%

%發票%


Sooo當我嘗試獲取所有這些值都匹配的文檔時,我得到一個錯誤的結果(但是,它總是有意義的:D)

信息:所述fn_split-函數採用searchString的作爲第一參數和第二參數是切片符號

select * 
from document 
WHERE 1 = (
    select case 
     when document.documenttitle like items then 1 else 0 end 
    from fn_split('invoice 4711', ' ') 
    where document.documenttitle like items 
); 

select * 
from document 
inner join fn_split('invoice 4711', ' ') 
on document.DocumentTitle like items 

這些查詢的兩個返回結果包含任何這些詞 - 不是兩者。 任何想法是什麼問題在這裏?或者查詢的工作原理是什麼?

+0

首先要有一個適當的項目結點表的數據結構。 –

+0

嗨,你必須搜索的是:%4711%發票%和%發票%4711%,所以你必須修改你的拆分功能,在「scrumbler」功能。 U應該嘗試類似於從fn_split('invoice 4711','')a,fn_split('invoice 4711','')選擇項目+'%'b其中a.items <> b.items for xml path(' ') – GigiS

+0

@GigiS如果'發票4711約翰史密斯德州'怎麼辦? – Serg

回答

2

數它們。

declare @arg varchar(100) ='invoice 4711'; 
select * 
from document 
cross apply ( 
select n=count(*) from fn_split(@arg, ' ') x where 
document.DocumentTitle like x.items) cnt 
where (select count(*) from fn_split(@arg, ' ')) = cnt.n; 
+0

不錯,完美無缺!謝謝 –

相關問題