0

I have a MySQL database with a column containing part numbers. Some of the part numbers contain spaces:

3864205010  J

When I query the database or search for the part in phpMyAdmin no results are returned.

Yet when I delete the 2 spaces and then type them again, the search returns a result.

This query does not return a result:

SELECT * 
FROM  `parts` 
WHERE  `part_no` LIKE  '3864205010  K'

This query returns the result:

SELECT * 
FROM  `parts` 
WHERE  `part_no` LIKE  '3864205010  K'

They look the same but in the second query I have deleted the 2 spaces before "K" and typed the spaces again.

14
  • not clear enough buddy Commented Apr 11, 2013 at 12:07
  • Your data type is varchar or text ? Show us your query Commented Apr 11, 2013 at 12:09
  • 1
    Actually just paste it in... someone else (even me) will format it for you if you have no idea. Commented Apr 11, 2013 at 12:16
  • 1
    this is probably not a space but a HTAB (ascii code 9) or even a line feed/carriage return (10 and 13). Copy paste in a good text editor, you'll see Commented Apr 11, 2013 at 12:39
  • 1
    Why you use LIKE for search exact result? Commented Apr 11, 2013 at 12:40

4 Answers 4

2

If you can use wildcard instead of spaces:

SELECT * 
FROM  `parts` 
WHERE  `part_no` LIKE  '3864205010%K'
Sign up to request clarification or add additional context in comments.

1 Comment

If I could give more than +1 I would. spot on buddy
0

This is probably not a space but a HTAB (ascii code 9) or even a line feed/carriage return (10 and 13). Copy paste in a good text editor, you'll see what it really is.

Now, regarding to your wonder about why it doesn't work even if it does look like a space, this is because every single character we see is interpreted by the engine (notepad, phpmyadmin, firefox... any software with text rendering)

What actually happens is that when the engine finds an ascii code, it transforms it into a visible character. The CHAR(9) for example is often transformed into a 'big space' usually equal to 2 or 4 spaces. But phpmyadmin might just decide to not do it that way.

Other example is the line feed (CHAR(10)). In a text editor it would be the signal that the line ends, and (under unix systems mostly) a new line has to start. But you can copy this line feed into a database field, you're just not sure about how it is going to render. Because they want you to see everything in the cell they may choose to render it as a space... but that's NOT a space if you look at the ascii code of it (and here there's no trick, all rendering engines will tell you the right ascii code).

This is important to always treat characters with their ascii codes.


there's an answer above that suggests using a wildcard instead of the spaces. That might match, or just might not. Let's say your string is '386420K5010', so it is not the one you're looking for, still the LIKE '3864205010%K' pattern would return it. The best is probably to use a regular expression or at least identify the fixed pattern of these strings.

1 Comment

ill probably clean up the data in my data database, just replace all <space><non-space> with <space><space>
0

yes as updated question if you wish to remove more space between which contents might be 3 or 4 space below query will use full to you

SELECT REPLACE( REPLACE( part_no, " ", " " ), " ", " " ) from parts.

let me know if it is work for you ?

Comments

0
SELECT * 
FROM  `parts` 
WHERE  REPLACE(REPLACE(`part_no`, CHAR(9), ''),' ','') LIKE  REPLACE(REPLACE('3864205010  K', CHAR(9), ''),' ','')

This will probably work if part_no and/or search string has tabs and/or spaces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.