select * from table_name where serial = '400 TZV 50'
I know this record exists, but the query return me an empty set.
For example, if you have the records:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( serial VARCHAR2(64) );
INSERT INTO table_name
SELECT '400 TZV 50' FROM DUAL UNION ALL -- Extra space in middle
SELECT '400 TZV 50 ' FROM DUAL UNION ALL -- Extra space at the end
SELECT '400 TZV 50' || CHR(13) || CHR(10) FROM DUAL UNION ALL -- CR/LF at the end
SELECT '400' || CHR(9) || 'TZV 50' FROM DUAL; -- Tab instead of space
To find the records that you think should match. You can either use LIKE:
SELECT serial, '400 TZV 50' AS match
FROM table_name
WHERE serial LIKE '400%TZV%50%'
or REGEXP_LIKE:
SELECT serial, '400 TZV 50' AS match
FROM table_name
WHERE REGEXP_LIKE( serial, '400\s+TZV\s+50\s*' )
Which both output:
Results:
| SERIAL | MATCH |
|--------------|------------|
| 400 TZV 50 | 400 TZV 50 |
| 400 TZV 50 | 400 TZV 50 |
| 400 TZV 50 | 400 TZV 50 |
| | |
| 400 TZV 50 | 400 TZV 50 |
then you can look and see whether the values are different.
If you cannot see why it is not matching then use the DUMP function to get the underlying byte values in the data types:
SELECT DUMP( serial ) AS serial, DUMP( '400 TZV 50' ) AS match
FROM table_name
WHERE REGEXP_LIKE( serial, '400\s+TZV\s+50\s*' )
Which outputs:
Results:
| SERIAL | MATCH |
|---------------------------------------------------|----------------------------------------------|
| Typ=1 Len=11: 52,48,48,32,84,90,86,32,32,53,48 | Typ=96 Len=10: 52,48,48,32,84,90,86,32,53,48 |
| Typ=1 Len=11: 52,48,48,32,84,90,86,32,53,48,32 | Typ=96 Len=10: 52,48,48,32,84,90,86,32,53,48 |
| Typ=1 Len=12: 52,48,48,32,84,90,86,32,53,48,13,10 | Typ=96 Len=10: 52,48,48,32,84,90,86,32,53,48 |
| Typ=1 Len=10: 52,48,48,9,84,90,86,32,53,48 | Typ=96 Len=10: 52,48,48,32,84,90,86,32,53,48 |
And you can see that the values are different (ignore the typ value as one is a string literal and the other is a VARCHAR2 stored in a table):
the first row has a different length from an extra space in the middle:
Typ= 1 Len=11: 52,48,48,32,84,90,86,32,32,53,48
Typ=96 Len=10: 52,48,48,32,84,90,86,32,53,48
^^ ^^
the second has a different length from an extra space at the end:
Typ= 1 Len=11: 52,48,48,32,84,90,86,32,53,48,32
Typ=96 Len=10: 52,48,48,32,84,90,86,32,53,48
^^ ^^^
the third has a different length from CR/LF characters at the end:
Typ= 1 Len=12: 52,48,48,32,84,90,86,32,53,48,13,10
Typ=96 Len=10: 52,48,48,32,84,90,86,32,53,48
^^ ^^^^^^
and the last row has an ASCII value of 9 (tab) in the 4th character when an ASCII value of 32 (space) is expected.
Typ= 1 Len=10: 52,48,48,9,84,90,86,32,53,48
Typ=96 Len=10: 52,48,48,32,84,90,86,32,53,48
^^
Once you have worked out why your query is not matching then you can adapt it to either match the actual data or, if the data is incorrectly formatted, you can fix the values in the table to be of the expected format.
select serial from table_name where serial like '400%TZV%50'and then check the ascii representation of the string...SELECT DUMP( serial ) AS serial, DUMP( '400 TZV 50' ) AS match FROM table_name WHERE REGEXP_LIKE( serial, '400\s+TZV\s+50\s*' )seems like a much simpler solution to looking at the raw byte values of the string and see why it is not matching. If it is matching too many rows then add ` AND ROWNUM = 1` to aid with debugging.