3
SELECT.....

 (v1.PCG like 'n0%'
or
v1.PCG like 'n1%'
or
v1.PCG like 'n2%'
or
v1.PCG like 'n3%'
or
v1.PCG like 'n4%'
or
v1.PCG like 'n5%'
or v1.PCG='N63Af1')

and v1.PCG not like 'N2D%'
and v1.PCG not like 'N2E%'
and v1.PCG not like 'N2C%'
and v1.PCG not like 'N2F%'
and v1.PCG not like 'N2J%'
and v1.PCG not like 'N2K%'
and v1.PCG not like 'N2U%'
and v1.PCG not like 'N2GC%'
and v1.PCG not like 'N2GD%'
and v1.PCG not like 'N2GH%'
and v1.PCG not like 'N2GJ%'
and v1.PCG not like 'N2GK%'
) as 'Value Of PN Orders',

from........

我已经完成了我的代码,但正在尝试找到一种更有效的方法,我看了看,但找不到另一种方法......有什么建议吗?

4

4 回答 4

4

Like如此支持字符类;

where v1.PCG like 'n[12345]%'
于 2013-06-05T13:22:29.937 回答
2

这种特殊情况可以使用通配符 - 并不需要正则表达式:

(v1.PCG LIKE 'n[0-5]%' OR v1.PCG='N63Af1')
AND v1.PCG NOT LIKE 'N2[CDEFJKU]%'
AND v1.PCG NOT LIKE 'N2G[CDHJK]%'

另一种不会改变语义但读起来更干净的替代方法是使用表变量或 CTE 来加入:

 DECLARE @match TABLE (Value varchar(5))
 INSERT @match VALUES 
    ('N0%'),
    ('N1%'),
    ('N2%'),
    ('N3%'),
    ('N4%'),
    ('N5%'),
    ('N63Af1')

 DECLARE @not TABLE (Value varchar(5))
 INSERT @not VALUES 
    ('N2D%'),
    ('N2E%'),
    ('N2C%'),
    ('N2F%'),
    ('N2J%'),
    ('N2K%'),
    ('N2U%'),
    ('N2GC%'),
    ('N2GD%'),
    ('N2GH%'),
    ('N2GJ%'),
    ('N2GK%')

SELECT ...
JOIN @match ON
     v1.PCG LIKE @match.Value
LEFT OUTER JOIN @not ON
     v1.PCG NOT LIKE @not.Value
WHERE
     @not.Value IS NULL

如果您的值是用户提供的,那将是一个很好的技巧。不过,在这种情况下,我认为通配符集更易于使用。

于 2013-06-05T13:30:52.610 回答
0

这看起来像是正则表达式的工作

于 2013-06-05T13:18:17.840 回答
0

您可以在 T-SQL 查询的 WHERE CLAUSE 中使用 CONTAINS() 谓词。

SELECT <<COLUMNS>>
FROM   <<TABLE>>
WHERE CONTAINS(<<COLUMN TO SEARCH>>, <<VALUE TO SEARCH>>);

在此处进一步阅读: MSDN:包含(T-SQL)

于 2013-06-05T13:25:52.103 回答