-3

that will sounds stupid, but I have a table with names, those names may finish with white space or may not. E.g. I have name ' dummy ', but even if in the query I write only ' dummy' it will find the record ' dummy '. Can I fix it somehow?

SELECT *
FROM MYTABLE where NAME=' dummy'

Thanks

Bear Bear
  • 77
  • 2
  • 9

1 Answers1

1

This is how SQL works (except Oracle), when you compare two strings the shorter one will be padded with blanks to the length of th 2nd string.

If you really need to consider trailings blanks you can switch to LIKE which doesn't follow that rule:

SELECT *
FROM MYTABLE where NAME LIKE ' dummy'

Of course, you better clean your data during load.

There's only one thing which is worse than trailing spaces, leading spaces (oh, wait a minute, you got them, too).

dnoeth
  • 59,503
  • 4
  • 39
  • 56
  • why it is worse to have leading spaces? – Bear Bear Oct 26 '16 at 19:39
  • #1: Because they are hard to spot, is it one or two or three of them? #2: You need to spcecify the exact number to be able to find them, e.g. `'dummy' <> ' dummy'` – dnoeth Oct 26 '16 at 19:41