开发者

sql- Like Query find

开发者 https://www.devze.com 2023-03-18 19:26 出处:网络
Would anyone know what to do with this I am doing a like query to select info. example: SELECT * FROM customers

Would anyone know what to do with this I am doing a like query to select info. example:

SELECT * 
  FROM customers
 WHERE customer_name LIKE '26%' 

which will return

26_xx
26_xx
265_xx

but i only want to display 26_xx

I have tried which was suggested from a site :

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

but that also returned

26_xx
2开发者_开发知识库6_xx
265_xx


In T-SQL you can use [] to escape the wildcard character _;

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

The ESCAPE version would be;

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


SELECT * 

FROM customers

WHERE customer_name LIKE '26_%'

Is this what you mean? Only where the names start "26_" or do I read your question wrong?


This should work:

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

Proof:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消