2016-10-22 66 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你的样本数据。