简体   繁体   English

在Oracle SQL Developer中搜索包含空格的字符串

[英]Searching string that contains spaces in Oracle SQL Developer

suppose you are selecting a row of a table that contains the field X equal to a certain String, and this string contains spaces 假设您正在选择表的一行,其中该表的字段X等于某个字符串,并且该字符串包含空格

in my case I've 就我而言

select * from table_name where serial = '400 TZV 50'

I know this record exists, but the query return me an empty set. 我知道该记录存在,但查询返回一个空集。

Anyone could help me? 有人可以帮助我吗?

 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 SQL小提琴

Oracle 11g R2 Schema Setup : Oracle 11g R2架构设置

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 : 您可以使用LIKE

SELECT serial, '400 TZV 50' AS match
FROM   table_name
WHERE  serial LIKE '400%TZV%50%'

or REGEXP_LIKE : 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: 如果看不到为什么不匹配,请使用DUMP函数获取数据类型中的基础字节值:

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): 并且您会看到值是不同的(忽略typ值,因为一个是字符串文字,另一个是存储在表中的VARCHAR2 ):

  • 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: 第三个字符的长度与结尾的CR / LF字符的长度不同:

     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. 当期望的ASCII值为32(空格)时,最后一行的第4个字符的ASCII值为9(制表符)。

     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 * from table_name where serial like '400%TZV%50';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM