在 JOIN 条件下使用 RTRIM

发布于 2024-12-04 22:02:37 字数 634 浏览 0 评论 0原文

我想将包含产品的两个表按其描述放在一起

表 1: [..fields..] [DESCRIPTION1] [..fields..]

表 2: [.. fields..] [DESCRIPTION2] [..fields..]

SELECT [..fields..] FROM TABLE1,TABLE2 
    INNER JOIN TABLE1 
    ON TABLE1.DESCRIPTION1 = TABLE2.DESCRIPTION2

但在很多情况下,两个表中都有以空格结尾的描述,我也想得到它们,这就是为什么我这样尝试:

SELECT [..fields..] FROM TABLE1,TABLE2 
    INNER JOIN TABLE1 
    ON TABLE1.DESCRIPTION1 = TABLE2.DESCRIPTION2 
    OR RTRIM(TABLE1.DESCRIPTION1) = RTRIM(TABLE2.DESCRIPTION2)

这不会产生错误,但不会带来更多数据。

有人能帮助我吗?

感谢您的每一次帮助:)

提前致谢,哈利

I want to put two tables which are containing Products together by their DESCRIPTIONs

TABLE 1: [..fields..] [DESCRIPTION1] [..fields..]

TABLE 2: [..fields..] [DESCRIPTION2] [..fields..]

SELECT [..fields..] FROM TABLE1,TABLE2 
    INNER JOIN TABLE1 
    ON TABLE1.DESCRIPTION1 = TABLE2.DESCRIPTION2

But in lots of cases, there are descriptions in both tables ending with an space, i also want to get them, thatswhy i tried it that way:

SELECT [..fields..] FROM TABLE1,TABLE2 
    INNER JOIN TABLE1 
    ON TABLE1.DESCRIPTION1 = TABLE2.DESCRIPTION2 
    OR RTRIM(TABLE1.DESCRIPTION1) = RTRIM(TABLE2.DESCRIPTION2)

Which produces no error, but does not bring more data.

Is anybody able to help me?

Every help is appreciated :)

Thanks in Advance, Harry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜巴黎 2024-12-11 22:02:37

问题可能是描述末尾的 CRLF 字符,这些字符在 SSMS 中看起来像空格,并且不会被 RTRIM() 删除。

要进行检查,请查看选择 where Description like '%' + char(13) + char(10) 是否返回任何内容。如果描述末尾似乎只有 1 个空格,请单独尝试 char(13)char(10)。最后,制表符 (char(9)) 也看起来像一个空格,因此您也可以尝试这样做。

如果它确实是这些字符中的任何一个,您可以使用 REPLACE 来删除它们:

REPLACE(REPLACE(REPLACE(MyField, CHAR(10), ''), CHAR(13), ''), CHAR(9), '')

我发现了此处

The problem might be CRLF characters at the end of the Description, which will look like spaces in SSMS and won't be removed by RTRIM().

To check, see if selecting where Description like '%' + char(13) + char(10) returns anything. If it looks like there is only 1 space at the end of the Description, try either char(13) or char(10) alone. Finally, a Tab (char(9)) would also look like a space, so you could try that as well.

If it does turn out to be any of these characters, you could use REPLACE to get rid of them:

REPLACE(REPLACE(REPLACE(MyField, CHAR(10), ''), CHAR(13), ''), CHAR(9), '')

which I found here.

山人契 2024-12-11 22:02:37

根据数据类型,您根本不需要 RTRIM()。我还想知道旧式和现代连接语法的混合是否会让您绊倒(尽管这应该会产生更多行,而不是更少)。这会产生什么结果?

SELECT [..columns..]
    FROM dbo.TABLE1 AS t1
    INNER JOIN dbo.TABLE2 AS t2
    ON RTRIM(t1.DESCRIPTION1) = RTRIM(t2.DESCRIPTION2);

Depending on the data type, you shouldn't need RTRIM() at all. I also wonder if your mix of old-style and modern join syntax is tripping you up (though that should yield more rows, not less). What does this yield?

SELECT [..columns..]
    FROM dbo.TABLE1 AS t1
    INNER JOIN dbo.TABLE2 AS t2
    ON RTRIM(t1.DESCRIPTION1) = RTRIM(t2.DESCRIPTION2);
顾铮苏瑾 2024-12-11 22:02:37

您不需要 OR

SELECT [..fields..] 
  FROM TABLE1 T1
       INNER JOIN TABLE2 T2
         ON RTRIM(T1.DESCRIPTION1) = RTRIM(T2.DESCRIPTION2)

You do not need the OR

SELECT [..fields..] 
  FROM TABLE1 T1
       INNER JOIN TABLE2 T2
         ON RTRIM(T1.DESCRIPTION1) = RTRIM(T2.DESCRIPTION2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文