2016-10-22 58 views
-1

我有一個表(defect),其中一列存儲一個文本。本文中的每一行代表一個版本。 (這是運行Microsoft SQL的clearquest數據庫,通過JDBC訪問)用新行分隔的模式查詢

例如,以下數據代表三個版本進行修復。

defect version_fixed 
1  2015.1.1 
2  2015.1.1\n2015.1.13 
3  2015.1.12\n2015.1.1 
4  2015.1.12\n2015.1.1\n2015.1.13 
5  2015.1.13\n2015.1.10 
5  2015.1.100 

正如您所看到的版本未按順序存儲。它可以出現在任何地方

我對包含「2015.1.1」的固定版本的所有行感興趣。但我的查詢或者得到更多的行或跳過一些

version_fixed like '%2016.1.1%' (gets row 5 as it matches the pattern) 
version_fixed like '%2016.1.1\n'(does not get any thing.) 

我找查詢得到確切名單2015年1月1日

defect version_fixed 
1  2015.1.1 
2  2015.1.1\n2015.1.13 
3  2015.1.12\n2015.1.1 
4  2015.1.12\n2015.1.1\n2015.1.13 

我怎樣才能查詢,其中文本與「精確匹配字符串,分隔以新行或文字結尾「。什麼是逃避新生產線的正確方法?

邊注:目前的解決方案是讓所有的記錄(包括不需要的一個,然後過濾掉不正確的結果)

+1

'2015.1.1'應在字符串的開頭,也可以是在通過一些X輸入 –

+0

同時添加預期的結果。增加了muiltiple擴展模式(附加新行等) –

回答

1

你可以試試這個。它依賴於Sql Server將行中添加換行符到字符串中。

create table defect(version_fixed varchar(max)) 
insert into defect(version_fixed) 
values ('2015.1.1') 
, ('2015.1.1 
2015.1.13') 
, ('2015.1.12 
2015.1.1') 
, ('2015.1.12 
2015.1.1 
2015.1.13') 
, ('2015.1.13 
2015.1.10') 
, ('2015.1.100') 

-- break to a new line and Sql Server will include the newline character in the string 
select * from defect where version_fixed like '%2015.1.1 
%' or version_fixed like '%2015.1.1' 
+0

這給了主意任何別的地方 – Jayan

0

您可以按以下:

WHERE '\' + version_fixed + '\' LIKE '%2015.1.1\%' 

該解決方案depands你的樣本數據。