欢迎!
我目前正在研究一些C#,我遇到了一个相当愚蠢的问题。我需要用数据库中的一些数据填充ListBox。问题是varchar过滤。我需要过滤代码并只显示正确的代码。
代码示例是:RRTT, RRTR, RT12, RQ00, R100, R200, R355, TY44, GG77 etc. Four digit codes.
我设法只使用简单的select * from table1 where code_field like 'R%'
过滤R代码,但我同时得到R000和RQ10,我需要得到R,然后只有数字。
所以从例子:
RRTT, RRTR, RT12, RQ00, R100, R200, R355
只要:
R100, R200, R355
任何想法我应该添加到like 'R%'
?
在SQL Server中,您可以使用通配符。这是一种方法:
where rcode like 'R___' and -- make sure code has four characters
rcode not like 'R%[^0-9]%' -- and nothing after the first one is not a number
要么:
where rcode like 'R[0-9][0-9][0-9]'
在其他数据库中,通常使用正则表达式而不是like
的扩展来执行此操作。
这里解决方案使用像
SELECT *
FROM (
VALUES ( 'R123'),
( 'R12T' ),
( 'RT12' )
) AS t(test)
where test like 'R[0-9][0-9][0-9]'
like 'S[0-9%]'
无论如何,朋友想出了解决方案,谢谢