1

When I compared text like this:

'a' = 'a '

the result is True - but I was expecting it to be false.

Do you know why the result is true? And can I do something to solve this issue?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MayXX
  • 19
  • 2
  • Does this answer your question? [Why the SQL Server ignore the empty space at the end automatically?](https://stackoverflow.com/questions/17876478/why-the-sql-server-ignore-the-empty-space-at-the-end-automatically) – Dale K Dec 07 '20 at 08:24

2 Answers2

0

You can use DATALENGTH()

Example

SELECT * FROM TableName WHERE DATALENGT('a') = DATALENGTH('a ')

DATALENGT('a') return the result as 1
and DATALENGTH('a ') return the result as 2, includes the space text.

Please check the db<>fiddle for reference.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
0

The LIKE operator does not pad the trailing spaces:

SELECT * FROM basePath WHERE 'a' LIKE 'a '

More information can be found here: https://dba.stackexchange.com/questions/56876/sql-server-auto-trim-of-varchar-value-in-equal-comparison-but-not-like-compariso

kaljak
  • 1,263
  • 1
  • 16
  • 34