SQL match on letter and number arrangement without using regular expressions

邮差的信 提交于 2019-12-24 01:36:11

问题


Is it possible to write an SQL statement for MySQL which finds all rows which match a specific arrangement of letters/numbers without using REGEXP?

i.e. SELECT myCol FROM myTable WHERE myCol='{number}{number}{letter}'

Should return 12X and 34Y but not 123X or 34YY

[I have asked a similar question before- ( SQL match on letter and number arrangement). The difference is that I have discovered that I cannot use regular expressions with the ADO.Net driver I am using. Whatsmore, I cannot update it since I am using Visual Studio 2003 which is not compatible with later versions.]


回答1:


Try this:

SELECT myCol 
FROM myTable 
WHERE SUBSTRING(myCol, 1 , 1) >= 'A' 
AND SUBSTRING(myCol, 1 , 1) <= 'Z' 
AND SUBSTRING(myCol, 2 , 1) >= 'A' 
AND SUBSTRING(myCol, 2 , 1) <= 'Z' 
AND SUBSTRING(myCol, 3 , 1) >= '0' 
AND SUBSTRING(myCol, 3 , 1) <= '9'



回答2:


Well, yes, but it would be very slow and resource-intensive ...

SELECT 
    myCol 
FROM 
    myTable 
WHERE 
    CHAR_LENGTH(myCol) = 3 
    AND SUBSTRING(myCol, 1, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') 
    AND SUBSTRING(myCol, 2, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0') 
    AND SUBSTRING(myCol, 3, 1) IN ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z')

That'll match "12X" and "34Y" but not "123X" or "34YY".




回答3:


Just to state the accepted answer explicitly:

SELECT myCol 
FROM myTable 
WHERE SUBSTRING(myCol, 1 , 1) BETWEEN 'A' AND 'Z' AND
SUBSTRING(myCol, 2 , 1) BETWEEN 'A' AND 'Z' AND
SUBSTRING(myCol, 3 , 1) BETWEEN '0' AND '9'

(mixture of Ken's answer and Andriy's suggestion)




回答4:


SELECT myCol 
FROM   myTable 
WHERE  ( myCol LIKE '12X%' OR myCol LIKE '34Y%' ) 
AND    myCol NOT LIKE '34YY%'

Rough example, but should help you figure the problem out. Can't help more since your example isn't that precise.




回答5:


If your query is as simple as your example (ie. no parameters) then can you use a Stored Procedure to return the results using Reg Exp? In that case the driver shouldn't know or care that you're using Reg Exp.




回答6:


Try this -

SELECT myCol FROM myTable WHERE myCol REGEXP '[[:alpha:]]{2}[[:digit:]]{1}'

http://dev.mysql.com/doc/refman/5.1/en/regexp.html




回答7:


If you are using EMS SQL Manager for MySQL, you can use RegEx to filter data.

SELECT col 
FROM tbl
WHERE col REGEXP '^[[:alpha:]]{2}[[:digit:]]{1}$'

RegEx:

^[[:alpha:]]{2}[[:digit:]]{1}$

^ = starts with

[[:alpha:]] = alphabet

[[:digit:]] = numbers

{n} = exactly n instances of

Hope this helps.




回答8:


You can test the first two digits using LIKE, and for the third use an ascii range. The first two digits can use an ascii range too, but LIKE should be better.

select *
from tbl
where length(n) = 3
  and '0123456789' like concat('%',mid(numcol,1,1),'%')
  and '0123456789' like concat('%',mid(numcol,2,1),'%')
  and ASCII(upper(MID(numcol,3,1))) between 65 and 90


来源:https://stackoverflow.com/questions/5064711/sql-match-on-letter-and-number-arrangement-without-using-regular-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!