我有一个表,其中一个字段需要清理。表的基本结构如下:
create table testComments(id int, comments text)在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"尽管如果注释按如下方式列出,则查询将找不到结果:
"This is an entry in the testComments crlf
crlf
crlf
That will not work"对于上述数据,查询将仅返回1个条目。您知道如何更改查询以返回计数2吗?
发布于 2013-07-03 00:28:56
使用您给我们的代码,您的查询应该可以正常工作。所以细节看起来是不同的,我怀疑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) + '%')发布于 2013-07-03 00:30:25
对于上述数据,查询将仅返回1个条目。您知道如何更改查询以返回计数2吗?
我想你误解了一些基本的东西。count -函数返回所有返回行的计数。在您的示例中,它仍然返回one行。
所以不管你有没有这个:
"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! (如果有一条记录包含此字符串)
EDIT:如果你真的想要计算每一行的字符数,你可以这样做:
SELECT LEN(REPLACE(myColumn, 'yourCharacter', '')) FROM ...发布于 2013-07-03 00:40:12
尝试使用虚拟表。在将您要查找的字符替换为空字符串后,我将注释字段和注释字段的长度之差相加。
SELECT SUM(DIF) FROM (
select
(
LEN(comments)
- LEN( REPLACE( REPLACE ( comments, char(13), ''),char(10),'') )
)
AS DIF
from #testComments
) as VT;https://stackoverflow.com/questions/17430497
复制相似问题