简体   繁体   English

如何在列中具有尾随空格值的SQL Server表中查找重复项

[英]How to find duplicates in a SQL Server table which has trailing spaces values in a column

select COL1, count(COL1)
from Table1
group by COL1
having count (COL1) > 1;

I have tried the above query and got some result based on data which do not have trailing spaces however the above query does not apply to data which has trailing spaces so I tried the below query and got no results. 我已经尝试了上述查询,并根据没有尾随空格的数据获得了一些结果,但是上述查询不适用于具有尾随空格的数据,因此我尝试了以下查询,但没有结果。 Please advice 请指教

select COL1, count(COL1)
from Table1
where COL1 in(select Ltrim(Rtrim(COL1))from Table1)
group by COL1
having count (COL1) > 1;

If you want to tally the text contents of COL1 ignoring leading and trailing whitespace, then just do that. 如果要计算COL1的文本内容而忽略COL1和结尾的空格,则只需这样做。 Use ltrim(rtrim(COL1)) when aggregating: 汇总时使用ltrim(rtrim(COL1))

select
    ltrim(rtrim(COL1)) AS COL1_trimmed
    count(*) cnt
from Table1
group by ltrim(rtrim(COL1))
having count(*) > 1;

In general, SQL Server ignores trailing spaces with varchar() . 通常,SQL Server使用varchar()忽略尾随空格。 However, it does not when using char() . 但是,使用char()时不会。 I am guessing the trailing "spaces" are not really spaces. 我猜尾随的“空格”不是真正的空格。

Here is an example . 这是一个例子

with t as (
      select cast('a' as varchar(255)) as x union all
      select cast('a  ' as varchar(255))
    )
select t.x, count(*), min(t.x + '|') , max(t.x + '|')
from t
group by t.x;

This returns: 返回:

a   2   "a  |"  "a|"

(I added the double quotes to clarify the results.) Note that one row is returned, not two. (我添加了双引号以澄清结果。)请注意,返回的是一行,而不是两行。 But the spaces really are at the end of the values. 但是空格确实位于值的末尾。

This leads me to suspect that the trailing characters are not spaces. 这使我怀疑结尾字符不是空格。

One way to investigate what they are is by using the ASCII() function. 研究它们是什么的一种方法是使用ASCII()函数。

Another way is to first remove the trailing and leading spaces from that column in your table. 另一种方法是首先从表中的该列中删除尾随和前导空格。

If COL1 is a VARCHAR type: 如果COL1是VARCHAR类型:

update Table1
set COL1 = rtrim(ltrim(COL1))
where COL1 != rtrim(ltrim(COL1));

If COL1 is a CHAR type then you only need to left trim: 如果COL1是CHAR类型,则只需要修剪一下:

update Table1
set COL1 = ltrim(COL1)
where COL1 != ltrim(COL1);

After that cleanup, you can just use a grouping query without trimming the column 清理之后,您可以只使用分组查询而无需修剪列

select COL1, count(*) as Total
from Table1
group by COL1
having count(*) > 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM