SQL喜欢过滤

问题描述 投票:0回答:3

欢迎!

我目前正在研究一些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 sql-server tsql sql-like varchar
3个回答
4
投票

在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的扩展来执行此操作。


0
投票

这里解决方案使用像

SELECT *
    FROM (
    VALUES ( 'R123'),
           ( 'R12T' ),
           ( 'RT12' )
    ) AS t(test)
    where test like 'R[0-9][0-9][0-9]'

-1
投票
like 'S[0-9%]' 

无论如何,朋友想出了解决方案,谢谢

© www.soinside.com 2019 - 2024. All rights reserved.