Yan
Yan

Reputation: 77

Right function + conversion

I'm comparing columns from two tables. However, one of them contains two-value numbers (like 01), while the other - one-value numbers (like 1).

As a result, the following condition doesn't work: `WHERE column_1 <> column_2 (results '01' and '1' are considered not equal).

I was told to add a zero to every number of the second column to the results and tried to use this: column_1 <> RIGHT(column_2('0' + CONVERT(VARCHAR(2), '1'), 2)) but the error message I get from SQL management Studio is that "right function requires two arguments".

Does anyone have the idea what can be done? Thanks!

Upvotes: 0

Views: 219

Answers (2)

shogun_kub
shogun_kub

Reputation: 1

As @JotaBe mentioned, you should compare integers, not stirngs. But still, your code in second case isn't syntactically correct, it should be:

column_1 <> RIGHT('0' + CONVERT(VARCHAR(2), column_2), 2)

But it won't work with negative numbers, for example.

Upvotes: 0

JotaBe
JotaBe

Reputation: 39055

You should cast the string to integers (CAST AND CONVERT) and compare the integer number:

where CAST(column_1 as INT) <> CAST(column_2 as INT)

Upvotes: 1

Related Questions