# String Comparison Using Inequalities in Excel

Alec Source

Can someone provide a reference to how excel compares strings using inequality operators in Excel? I have found some unexpected behavior:

In the below examples, the first two columns are strings, column C is of the expression `=A2<B2`, the third column is the expected behavior, and the final column is whether the expected behavior matches the actual.

``````Comparison  Benchmark   Is A < B?   Expected    Pass?
Q1 2009     Q4 2012       TRUE      TRUE        TRUE
Q2 2009     Q4 2012       TRUE      TRUE        TRUE
Q3 2009     Q4 2012       TRUE      TRUE        TRUE
Q4 2009     Q4 2012       TRUE      TRUE        TRUE
Q1 2010     Q4 2012       TRUE      TRUE        TRUE
Q2 2010     Q4 2012       TRUE      TRUE        TRUE
Q3 2010     Q4 2012       TRUE      TRUE        TRUE
Q4 2010     Q4 2012       TRUE      TRUE        TRUE
Q1 2011     Q4 2012       TRUE      TRUE        TRUE
Q2 2011     Q4 2012       TRUE      TRUE        TRUE
Q3 2011     Q4 2012       TRUE      TRUE        TRUE
Q4 2011     Q4 2012       TRUE      TRUE        TRUE
Q1 2012     Q4 2012       TRUE      TRUE        TRUE
Q2 2012     Q4 2012       TRUE      TRUE        TRUE
Q3 2012     Q4 2012       TRUE      TRUE        TRUE
Q4 2012     Q4 2012       FALSE     FALSE       TRUE
Q1 2013     Q4 2012       TRUE      FALSE       FALSE
Q2 2013     Q4 2012       TRUE      FALSE       FALSE
Q3 2013     Q4 2012       TRUE      FALSE       FALSE
Q4 2013     Q4 2012       FALSE     FALSE       TRUE
Q1 2014     Q4 2012       TRUE      FALSE       FALSE
Q2 2014     Q4 2012       TRUE      FALSE       FALSE
Q3 2014     Q4 2012       TRUE      FALSE       FALSE
``````

For dates before the benchmark, it seems like Excel compares the whole string, whereas after the benchmark date, Excel only compares the string until it finds a single character less than the analogous character in the comparison.

excelexcel-formulastring-comparison

answered 6 years ago ikh #1

What Excel does here is it compares the two strings character by character left to right until it finds a mismatch, and then the string with higher value character becomes the greater string. This is the standard method to compare strings -- it's used in pretty much all programming languages, databases and other software packages.

So, for example, for the first line the comparison would go like this:

``````Comparison  Benchmark   Outcome
Q           Q        Equal
1           4        Benchmark is greater - done comparing
[space]     [space]     Ignored
2           2        Ignored
0           0        Ignored
0           1        Ignored
9           2        Ignored
``````

For the fourth line it would be:

``````Comparison  Benchmark   Outcome
Q           Q        Equal
4           4        Equal
[space]     [space]     Equal
2           2        Equal
0           0        Equal
0           1        Benchmark is greater - done comparing
9           2        Ignored
``````

And for the last line it would be:

``````Comparison  Benchmark   Outcome
Q           Q        Equal
3           4        Benchmark is greater - done comparing
[space]     [space]     Ignored
2           2        Ignored
0           0        Ignored
1           1        Ignored
4           2        Ignored
``````