保护特殊字符

下列查询使用 ESCAPE 与 LIKE 或 MATCHES 配合使用,以便您可以保护特殊字符,使它们不会被误认为是通配符。

图: 查询

SELECT * FROM cust_calls
          WHERE res_descr LIKE '%!%%' ESCAPE '!'; 
ESCAPE 关键字指定包含下一个字符的转义字符(在本示例中为 !)以便将它解释为数据而不是通配符。在该示例中,转义字符导致将中间的百分号(%)当作数据。通过使用 ESCAPE 关键字,您可以使用 LIKE 通配符百分号(%)在 res_descr 列中搜索百分号(%)的出现次数。查询检索下列显示的行。

图: 查询结果

customer_num   116
          call_dtime     1997-12-21 11:24
          user_id        mannyn
          call_code      I
          call_descr     Second complaint from this customer! 
          Received two cases righthanded outfielder 
          glove (1 HRO) instead of one case lefties.
          res_dtime      1997-12-27 08:19
          res_descr      Memo to shipping (Ava Brown) to send case 
          of lefthanded gloves, pick up wrong case;
          memo to billing requesting 5% discount to 
          placate customer due to second offense 
          and lateness of resolution because of
          holiday.