Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RTRIM in JOIN condition

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

like image 448
Harry Avatar asked Feb 05 '26 13:02

Harry


2 Answers

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.

like image 176
Jeff Ogata Avatar answered Feb 08 '26 05:02

Jeff Ogata


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);
like image 43
Aaron Bertrand Avatar answered Feb 08 '26 04:02

Aaron Bertrand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!