查询由新行分隔的模式

Query for pattern separated by new lines

我有一个 table (defect ),其中一列存储文本。此文本中的每一行代表一个版本。 (这是 clearquest 数据库 运行 微软 SQL,通过 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 

如何查询文本与 "exact string, delimited by new line or end of text" 匹配的位置。转义新行的正确方法是什么?

旁注:当前的解决方案是获取所有记录(包括不需要的记录,然后过滤掉不正确的结果)

您可以如下:

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

此解决方案取决于您的样本数据。

你可以试试这个。它依赖于 Sql 服务器在你换行时将换行符添加到字符串中。

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'