sql- Like Query find

时间:2011-07-08 15:31:44

标签: sql

有人知道如何处理这个我正在做一个类似查询来选择信息。例如:

SELECT * 
  FROM customers
 WHERE customer_name LIKE '26%' 

将返回

26_xx
26_xx
265_xx

但我只想显示26_xx

我尝试过从网站上建议的内容:

SELECT * 
  FROM customers
 WHERE customer_name LIKE 'H%!%' escape '!';

但也返回了

26_xx
26_xx
265_xx

3 个答案:

答案 0 :(得分:4)

在T-SQL中,您可以使用[]来转义通配符字符_;

SELECT * FROM customers
    WHERE customer_name LIKE '26[_]%'

ESCAPE版本将是;

SELECT * FROM customers
    WHERE customer_name LIKE '26!_%' ESCAPE '!'

答案 1 :(得分:1)

SELECT * 

FROM customers

WHERE customer_name LIKE '26_%'
这是什么意思?只有名称以“26_”开头或者我的问题读错了吗?

答案 2 :(得分:0)

这应该有效:

SELECT * 
FROM customers
WHERE customer_name LIKE '26[_]%' 
  and len(customer_name) = 5

证明:

select
  sample_data.*
from
  (
  select '26_55' as customer_name
  union
  select '26_abc'
  union
  select '265_22'
  union
  select '26-22'
  union
  select 'abc'
  union
  select '26_18'
  ) sample_data
where sample_data.customer_name like '26[_]%'
  and len(sample_data.customer_name) = 5