在sql server表中搜索ASCII值

在sql server表中搜索ASCII值,sql,sql-server,Sql,Sql Server,我有张桌子,其中一块地需要清理。基本表格结构如下: create table testComments(id int, comments text) "This is an entry in the testComments crlf crlf crlf That will not work" 在comments列中,一些行必须包含许多“回车”(char(13))和“换行”(char(10))。如果每行有多个分组,我需要能够修改它。到目前为止,我的基本select语句如下所示: sele

我有张桌子,其中一块地需要清理。基本表格结构如下:

create table testComments(id int, comments text)
"This is an entry in the testComments crlf
crlf
crlf
That will not work"
在comments列中,一些行必须包含许多“回车”(char(13))和“换行”(char(10))。如果每行有多个分组,我需要能够修改它。到目前为止,我的基本select语句如下所示:

  select count(id)
  from testComments
  where comments like('%' +  char(13) + char(10) + char(13) + char(10) + '%')
此查询将查找结果

"This is a entry in the testComments crlf
crlf
In the comments field that works"
虽然如果注释如下所示,查询将找不到结果:

create table testComments(id int, comments text)
"This is an entry in the testComments crlf
crlf
crlf
That will not work"

对于上述数据,查询将只返回1个条目的计数。您知道如何将查询更改为返回计数2吗?

使用您提供的代码,您的查询应该可以正常工作。所以细节似乎有所不同,我怀疑GolezTrol的评论是正确的——一些CRLF对实际上只是单独的CR或LF角色

试试这个:

select count(id)
  from #testComments
  where comments like('%' +  char(13) + char(10) + char(13) + char(10) + '%')
     or comments like('%' +  char(10) + char(10) + '%')
     or comments like('%' +  char(13) + char(13) + '%')
对于上述数据,查询将只返回1个条目的计数。知道如何更改查询以返回计数2吗

我想你误解了一些基本的东西。 COUNT函数返回所有返回行的计数。在您的示例中,它仍然返回一行

所以不管你有没有这个:

"This is an entry in the testComments crlf
crlf
That will not work"
或者这个:

"This is an entry in the testComments crlf
crlf
crlf
crlf
crlf
That will not work"
计数仍将返回1(如果有一条记录包含此字符串)

编辑:如果您真的想计算每行的字符数,请执行以下操作:

SELECT LEN(REPLACE(myColumn, 'yourCharacter', '')) FROM ...

尝试使用虚拟表。在将要查找的字符替换为空字符串后,我将注释字段和注释字段的长度差异相加

SELECT SUM(DIF) FROM (
select    
(
  LEN(comments)
          - LEN( REPLACE(   REPLACE ( comments, char(13), ''),char(10),'') ) 
) 
 AS DIF    
 from #testComments
 ) as VT;

返回计数2是什么意思?你只需要逻辑来选择后一个例子,对吗?是的,我只需要逻辑来选择后一个例子。这个查询应该返回每个文本。你确定没有别的问题吗?也许第二个文本只包含Char(10)(lf)。为了说明@GolezTrol的观点,这里有一个。我很确定他不想简单地检测带有CR或lf的字符串,而是检测带有一对相邻的CRLF的字符串,它们之间没有任何内容。(即他在寻找空行)然后他只需将字符(13)/字符(10)与感兴趣的字符交换即可。至少,这应该足够开始了。这不是他的问题的原因吗?如果不回答这个问题,你是如何帮助我的?我会更具体地回答我的问题。这是正确的,我知道这很简单。我就是想不出来。非常感谢。