如何在子字符串SQL中搜索特殊字符

时间:2019-05-11 09:47:13

标签: sql

您好,基本上我有一个付款列,其值为0,$ [amt]或[amt]%,我如何搜索包含%的子字符串?现在我正在做

select * from table where not Payment like '$%' and not Payment =0

检索子字符串中包含%的所有行

3 个答案:

答案 0 :(得分:2)

您可以尝试使用%\%(转义%)

select * from table where not Payment like '%\%' and not Payment =0

答案 1 :(得分:2)

MySQL 5.6

SELECT
    * 
FROM 
    PAYMENT 
WHERE 
    AMOUNT NOT LIKE '$\%%' 
AND 
    CONVERT(SUBSTRING(AMOUNT, 2), SIGNED INTEGER) = 0;

sqlfiddle

Oracle 11g R2

SELECT
   *
FROM 
    PAYMENT 
WHERE 
    AMOUNT NOT LIKE '$\%%' 
AND
    SUBSTR(AMOUNT, 2) LIKE '0' ESCAPE '\';

sqlfiddle

答案 2 :(得分:1)

这回答了用SQLite标记的原始问题。
您可以使用ESCAPE(在SQL As Understood By SQLite处找到更多信息):

select * from tablename where Payment like '%#%' ESCAPE '#'

转义字符'#'之后的字符被视为字符串文字,而不是通配符。
请参见demo

CREATE TABLE tablename (
    Payment TEXT
);
INSERT INTO tablename (Payment) VALUES 
 ('0'),
 ('15%'),
 ('$55'),
 ('3%');

结果:

| Payment |
| ------- |
| 15%     |
| 3%      |