Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8570259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:31:04+00:00 2026-06-11T18:31:04+00:00

I am building a database in which trailing space is important to the results.

  • 0

I am building a database in which trailing space is important to the results. When I query for a result I find that

SELECT * where `field` = 'a ' 

Returns a result when there is a field who’s value is ‘a’. I want the trailing space to matter in the result set. I have tried using Char, Varchar, Text, and Blob. I will note that this field is the index of my table.

Can someone show me how to query in a way that makes trailing (and/or leading) spaces count? Do I need to format my table in any special way to make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:31:06+00:00Added an answer on June 11, 2026 at 6:31 pm

    This behaviour is by design, not only in MySQL.

    You can work around it in comparisons by using BINARY:

    mysql> select version(), 'a' = 'a ', BINARY 'a' = BINARY 'a ';
    +-------------+------------+--------------------------+
    | version()   | 'a' = 'a ' | BINARY 'a' = BINARY 'a ' |
    +-------------+------------+--------------------------+
    | 5.5.25a-log |          1 |                        0 |
    +-------------+------------+--------------------------+
    1 row in set (0.00 sec)
    

    but not much more. This will help you with SELECTs if whitespaces appear e.g. in user input to a search; but if you want to actually input whitespace-trailed information, it will be a problem (you can’t have an index with both ‘a’ and ‘a ‘).

    See also

    Trailing whitespace in varchar needs to be considered in comparison

    You could conceivably reverse the strings in that column, and reverse them back when displaying them. Of course this will wreck any ordering based on that column, but if you only test equality or substring existence, it just might work. Leading spaces do count.

    For equality searches you might also store the base64 encoding of the string, which ought to maintain the lexicographical order (i.e., the order between a and b ought to be maintained between base64(a) and base64(b)). Or you might append a terminator on the string (“\n” could do well and not appear in searches).

    Finally, but it’s risky because humans can’t tell the difference, you could replace spaces with the UTF8 char(49824):

    mysql> select concat ('\'a', char(49824),'\'') AS tricked,
                  concat ('\'a', ' '        ,'\'') as honest,
                  concat ('\'a', char(49824),'\'') =
                  concat ('\'a', ' '        ,'\'') as equals;
    
    +---------+--------+--------+
    | tricked | honest | equals |
    +---------+--------+--------+
    | 'a '    | 'a '   |      0 |
    +---------+--------+--------+
    1 row in set (0.00 sec)
    

    The rows seem to be equal, but they are not. Note that in HTML the space is a space, and 49824 is   (nonbreaking space). This affects functions that convert to and fro HTML, and the nbsp being actually an UTF8 codepoint means that honest string is two bytes, but length of tricked string is actually three.

    Finally you can declare the column VARBINARY instead of VARCHAR, thus completely hiding what’s happening. It looks like the easiest solution, but I fear it might bite you some weeks or months down the line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a word anagram program that uses a database which contains one simple
I'm building an application around a database(which was built by someone else, so changing
I am building an application which stores GPS locations in a SQLite database and
I am building an application in C++ which uses sqlite3 as an embedded database.
Scenario I am building a database that contains a series of different tables. These
I'm building a rather large database - which has around 6.9 million records. A
I'm building an application on top of a legacy database (which I cannot change).
I'm currently building a small database which represents a pipe network. I have divided
I am building a database which consists of a table of category, customer table
I'm building a database with several collections. I have unique strings that I plan

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.