String comparison using SQL server

When you compare 2 columns of string data and one column is defined let's say

  char(4) and the other column char(5), does the comparison operation make the smaller column the same size as the larger column in the comparison operation?
Lawrence AverySystem DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
These two columns will never could be compared unless you use substring, left, right, trim, etc. as applicable.
PortletPaulEE Topic AdvisorCommented:
not sure how to answer as you don't specify the comparison operation

From this data
CREATE TABLE Table1
    ([string4] char(4), [string5] char(5))
;
    
INSERT INTO Table1
    ([string4], [string5])
VALUES
    ('abcd', 'abcde'),
    ('abcd', 'abcd'),
    ('abc', 'abcde'),
    ('abc', 'abcd'),
    ('abc', 'abc'),
    ('a', 'abcde'),
    ('abcd', 'ab'),
    ('abcd', 'a'),
    ('', ''),
    (null, null);

Open in new window

I get this result:
| string4 | string5 |       |             |          |  |
|---------|---------|-------|-------------|----------|--|
|    abcd |   abcde |       | left 4 of 5 | 5 like 4 |  |
|    abcd |   abcd  | equal | left 4 of 5 | 5 like 4 |  |
|    abc  |   abcde |       |             |          |  |
|    abc  |   abcd  |       |             |          |  |
|    abc  |   abc   | equal | left 4 of 5 | 5 like 4 |  |
|    a    |   abcde |       |             |          |  |
|    abcd |   a     |       |             |          |  |
|    abcd |   ab    |       |             |          |  |
|    abcd |   a     |       |             |          |  |
|         |         | equal | left 4 of 5 | 5 like 4 |  |
|  (null) |  (null) |       |             |          |  |

Open in new window

using this query
select
       string4
     , string5
     , case when string4 = string5 then 'equal' else '' end
     , case when left(string5,4) = string4 then 'left 4 of 5' else '' end
     , case when string5 like (string4 + '%') then '5 like 4' else '' end
     , case when string4 like (string5 + '%') then '4 like 5' else '' end
from table1

Open in new window

so, even though they are different fixed lengths, they can be equal or like or affected by functions; i.e. there is an almost endless variety of comparisons
Mike EghtebasDatabase and Application DeveloperCommented:
After seeing Paul's comment, I see that they could be compared using like.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Trent SmithCommented:
I am not positive that you can use a like command since if column A is abcd and column B is abcde wouldn't the column b information be like the first 4 characters of column a?  I would think you should do a column a = column b.  Otherwise it is hard to say what results you will get with a like command.
Mike EghtebasDatabase and Application DeveloperCommented:
@Trent Smith,

Sorry for changing your example a bit to demo when Like could work.

Col1 = 'abcde'            char(5)
Col2 = 'abcd'              char(4)

Then  ... Where Col1 Like Col2 + '%'

should work. Do you agree?
Lawrence AverySystem DeveloperAuthor Commented:
I am talking about the equal operation.
Mike EghtebasDatabase and Application DeveloperCommented:
oh, sorry, I was responding to your statement "... I am not positive that you can use a like command..."
PortletPaulEE Topic AdvisorCommented:
Perhaps you would relook at the small test cases I supplied earlier.
Scott PletcherSenior DBACommented:
does the comparison operation make the smaller column the same size as the larger column in the comparison operation?

Yes.  SQL effectively pads the shorter column with spaces.  When doing character comparisons, one trailing space = 2 trailing spaces = 1000 trailing spaces = ...

SELECT CASE WHEN 'AB' + SPACE(4) = 'AB' + SPACE(1000) THEN 'Match' ELSE 'No Match' END

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperCommented:
Thanks Scott,

I also tried a simpler version; as expected I got:

SELECT CASE WHEN 'AB  ' = 'AB'  THEN 'Match' ELSE 'No Match' END

Match
Trent SmithCommented:
To be honest with you I was playing devils advocate to this question.  Every time I have done a comparison I have been doing it using a key from the tables.
Vitor MontalvãoMSSQL Senior EngineerCommented:
metro156, do you still need help with this question?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.