执行不区分空格的查询 [英] performing a whitespace-insensitive query

查看:103
本文介绍了执行不区分空格的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试根据

空白不敏感的比较来编写一个将连接行的查询。例如,如果一行具有值

''a< space> b''而另一行具有值''< space>< space> b''我希望它们是

被认为是相等的(但与''ab'不同)


如果有帮助,我很乐意做一些T-SQL。此外,我在相关列上有一个全文索引

,但请注意我正在比较每个

其他行,而不是固定字符串。


这是一次性的工作,所以如果没有明显的方法在

数据库上做,我只会bcp out数据并敲掉一些perl或者什么

来做。所有其他条件相同我宁愿在数据库上做到这一点

虽然。


非常感谢


Andy

解决方案



" Andy Fish" < AJ **** @ blueyonder.co.uk>在消息中写道

新闻:QT ********************* @ news-text.cableinet.net ...



我正在尝试编写一个基于空白不敏感比较来连接行的查询。例如,如果一行具有值
''a< space> b''而另一行具有值''a< space>< space> b''我希望它们被认为是相等的(但与''ab''不一样)

如果有帮助,我很乐意做一些T-SQL。此外,我在相关列上有一个全文索引
,但请注意,我正在比较每个
其他行,而不是固定字符串。

这是为一次性的工作,所以如果没有明显的方法在
数据库上进行,我将只是bcp out数据并敲掉一些perl或者什么来做它。所有其他条件相同我宁愿在
数据库上做。

非常感谢

Andy




如果你想在数据库中使用它,并假设你有SQL2000,那么

一种可能性就是编写一个用户定义的函数来迭代字符串

并使用标准的

字符串函数将所有空格字符缩减为单个空格。如果你只有空格(即没有标签或换行符),那么

甚至一些嵌套的REPLACE()函数也可以工作。


另一种方法是写使用sp_OA%过程实例化Regex COM

对象的函数,并使用常规的

表达式执行相同的操作。但是这两种方法都会非常慢(除非你使用你的UDF作为计算列并将其编入索引),而第二种方法

需要sysadmin权限。 />

我怀疑,你的BCP和Perl方法可能是

最快最简单的解决方案。


西蒙


SELECT *

来自A

INNER JOIN B

ON替换(

替换(

替换(

替换(

替换(

替换( A.col,复制(''X'',132),''X'')

,复制(''X'',21),''X'')

,复制(''X'',6),''X'')

,复制(''X'',3),''X'')

,复制(''X'',2),''X'')

,复制(''X'',2),''X'' )

=替换(

替换(

替换(

替换(

)回覆地方(

替换(B.col,复制(''X'',132),''X'')

,复制(''''', 21),''X'')

,复制(''X'',6),''X'')

,复制(''X' ',3),''X'')

,复制(''X'',2),''X'')

,复制('' X'',2),''X'')

希望这会有所帮助,

Gert-Jan


Andy Fish写道:




我正在尝试编写一个基于空白不敏感比较来连接行的查询。例如,如果一行具有值
''a< space> b''而另一行具有值''a< space>< space> b''我希望它们被认为是相等的(但与''ab''不一样)

如果有帮助,我很乐意做一些T-SQL。此外,我在相关列上有一个全文索引,但请注意,我正在比较每个
其他行,而不是固定字符串。

这是为一次性的工作,所以如果没有明显的方法在
数据库上进行,我将只是bcp out数据并敲掉一些perl或者什么来做它。所有其他事情都是平等的我宁愿在数据库上做。

非常感谢

Andy



嗯,我再次发布了。将所有X替换为解决方案中的空间

以下它应该有效...


Gert-Jan

Gert-Jan Strik写道:


SELECT *
FROM A
INNER JOIN B
ON替换(
替换(
替换(<更换(
替换(替换(A.col,复制(''X'',132),''X'')
,复制('''''' ,21),''X'')
,复制(''X'',6),''X'')
,复制(''X'',3),'' X'')
,复制(''X'',2),''X'')
,复制(''X'',2),''X'')
=更换(
更换(
更换(
更换(
替换(
替换(B.col,复制(''X'',132), ''X'')
,复制('''X'' ,21),''X'')
,复制(''X'',6),''X'')
,复制(''X'',3),'' X'')
,复制(''X'',2),''X'')
,复制(''X'',2),''X'') Gert-Jan

Andy Fish写道:



我正在尝试编写一个基于空白不敏感比较来连接行的查询。例如,如果一行具有值
''a< space> b''而另一行具有值''a< space>< space> b''我希望它们被认为是相等的(但与''ab''不一样)

如果有帮助,我很乐意做一些T-SQL。此外,我在相关列上有一个全文索引,但请注意,我正在比较每个
其他行,而不是固定字符串。

这是为一次性的工作,所以如果没有明显的方法在
数据库上进行,我将只是bcp out数据并敲掉一些perl或者什么来做它。所有其他事情都是平等的我宁愿在数据库上做。

非常感谢

Andy



Hi,

I am trying to concoct a query that will join rows on the basis of a
whitespace insensitive comparison. For instance if one row has the value
''a<space>b'' and another has the value ''a<space><space>b'' I want them to be
considered equal (but not the same as ''ab'')

I am happy to do some T-SQL if that helps. Also I have a full-text index on
the column in question, but note that I am comparing rows against each
other, not to a fixed string.

This is for a one-off job, so if there is no obvious way to do it on the
database, I will just bcp out the data and knock up some perl or something
to do it. All other things being equal I would rather do it on the database
though.

Many thanks

Andy

解决方案


"Andy Fish" <aj****@blueyonder.co.uk> wrote in message
news:QT*********************@news-text.cableinet.net...

Hi,

I am trying to concoct a query that will join rows on the basis of a
whitespace insensitive comparison. For instance if one row has the value
''a<space>b'' and another has the value ''a<space><space>b'' I want them to be
considered equal (but not the same as ''ab'')

I am happy to do some T-SQL if that helps. Also I have a full-text index on the column in question, but note that I am comparing rows against each
other, not to a fixed string.

This is for a one-off job, so if there is no obvious way to do it on the
database, I will just bcp out the data and knock up some perl or something
to do it. All other things being equal I would rather do it on the database though.

Many thanks

Andy



If you want to it in the database, and assuming that you have SQL2000, then
one possibility is to write a user-defined function to iterate over a string
and reduce all whitespace characters to a single space, using the standard
string functions. If you only have spaces (ie no tabs or newlines), then
even some nested REPLACE() functions might work.

Another way would be to write a function which instantiates the Regex COM
object using the sp_OA% procedures, and does the same thing with a regular
expression. But both these approaches would be very slow (unless perhaps you
used your UDF as a computed column and indexed it), and the second one
requires sysadmin permissions.

I suspect, though, that your BCP and Perl approach would probably be the
quickest and easiest solution.

Simon


SELECT *
FROM A
INNER JOIN B
ON Replace(
Replace(
Replace(
Replace(
Replace(
Replace(A.col, replicate(''X'',132),''X'')
, replicate(''X'', 21),''X'')
, replicate(''X'', 6),''X'')
, replicate(''X'', 3),''X'')
, replicate(''X'', 2),''X'')
, replicate(''X'', 2),''X'')
= Replace(
Replace(
Replace(
Replace(
Replace(
Replace(B.col, replicate(''X'',132),''X'')
, replicate(''X'', 21),''X'')
, replicate(''X'', 6),''X'')
, replicate(''X'', 3),''X'')
, replicate(''X'', 2),''X'')
, replicate(''X'', 2),''X'')
Hope this helps,
Gert-Jan

Andy Fish wrote:


Hi,

I am trying to concoct a query that will join rows on the basis of a
whitespace insensitive comparison. For instance if one row has the value
''a<space>b'' and another has the value ''a<space><space>b'' I want them to be
considered equal (but not the same as ''ab'')

I am happy to do some T-SQL if that helps. Also I have a full-text index on
the column in question, but note that I am comparing rows against each
other, not to a fixed string.

This is for a one-off job, so if there is no obvious way to do it on the
database, I will just bcp out the data and knock up some perl or something
to do it. All other things being equal I would rather do it on the database
though.

Many thanks

Andy



Hmmm, again I posted to fast. Replace all X with space in the solution
below and it should work...

Gert-Jan
Gert-Jan Strik wrote:


SELECT *
FROM A
INNER JOIN B
ON Replace(
Replace(
Replace(
Replace(
Replace(
Replace(A.col, replicate(''X'',132),''X'')
, replicate(''X'', 21),''X'')
, replicate(''X'', 6),''X'')
, replicate(''X'', 3),''X'')
, replicate(''X'', 2),''X'')
, replicate(''X'', 2),''X'')
= Replace(
Replace(
Replace(
Replace(
Replace(
Replace(B.col, replicate(''X'',132),''X'')
, replicate(''X'', 21),''X'')
, replicate(''X'', 6),''X'')
, replicate(''X'', 3),''X'')
, replicate(''X'', 2),''X'')
, replicate(''X'', 2),''X'')

Hope this helps,
Gert-Jan

Andy Fish wrote:


Hi,

I am trying to concoct a query that will join rows on the basis of a
whitespace insensitive comparison. For instance if one row has the value
''a<space>b'' and another has the value ''a<space><space>b'' I want them to be
considered equal (but not the same as ''ab'')

I am happy to do some T-SQL if that helps. Also I have a full-text index on
the column in question, but note that I am comparing rows against each
other, not to a fixed string.

This is for a one-off job, so if there is no obvious way to do it on the
database, I will just bcp out the data and knock up some perl or something
to do it. All other things being equal I would rather do it on the database
though.

Many thanks

Andy



这篇关于执行不区分空格的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆