2016-07-05 186 views
0

我有一张填充公司代码前缀(cp-partnum,ag-partnum,ff-partnum)的表格。我想选择所有以'cp-partnum'开头的代码,其中partnum不以'I'或'i'开头。所以'cp-aaa'是可以接受的,但'cp-iaa'不是。这怎么能实现?喜欢但不喜欢

编辑:要清楚,我想只有开头的代码“CP-[信其中字母不是‘我’或‘我’]”

回答

1

你可以这样做:

where col like 'cp-%' and 
     col not like 'cp-l%' and 
     col not like 'cp-i%' 

对于多个前缀:

where left(col, 2) in ('cp', 'ag', 'ff') and 
     substring(col, 4, 1) not in ('l', 'i') and 
     col like '__-%' 
+0

它并不总是cp。公司代码就像cp-,ag-,ff- .. – MeisterAjit

2

使用下面的查询:

select * 
from companies 
where company_code like '__-%' 
and company_code not like '__-I%' 
and company_code not like '__-i%' 
+0

我有多个公司。(ag-,ca-等) –

+0

是的。此查询将使用ag-,ca-等获取所有代码。 – MeisterAjit

2

您可以使用SUBSTRINGNOT IN只有1 LIKE

WHERE YourColumn LIKE 'cp-%' 
    AND SUBSTRING(YourColumn from 4 for 1) NOT IN('i','l') 
+0

您将需要使用4而不是3。请参阅['SUBSTRING'](http://www.firebirdsql.org/file/documentation/reference_manuals/ fblangref25-en/html/fblangref25-functions-scalarfuncs.html#fblangref25-functions-scalarfuncs-substring),该位置是基于1的(比如(几乎?)SQL标准中的所有内容)。 –

+0

是的,但'FROM'让我感到困惑了一点..不知道它是从3包括3还是从3并转发@MarkRotteveel – sagi

+0

请参阅doc中的引用:_“此函数返回从字符位置开始的子字符串'startpos'(第一个位置是1)。“_ –

3

您可以使用SIMILAR TO和(SQL)的正则表达式:

yourcolumn SIMILAR TO 'cp-[^iI]%' 

如果公司总是2(阿尔法)的字符,你可以这样做:

yourcolumn SIMILAR TO '[:ALPHA:]{2}-[^iI]%' 

如果公司代码可以是任何两个字符您可以使用

yourcolumn SIMILAR TO '__-[^iI]%' 

对于更复杂的模式,请研究文档。 Firebird 2.5开始提供SIMILAR TO

+0

我正在使用https://www.fishbowlinventory.com/,显然它不支持2.5。 –

+0

@DanSerio如果可以升级到Firebird 2.5,你需要问鱼缸。 –

0

我想我不需要支持小写字母,所以下面的工作。

SELECT * FROM product 
WHERE product.num LIKE 'CP-%' 
AND product.num NOT IN (SELECT product.num FROM product 
    WHERE product.num LIKE 'CP-I%') 
+0

这是非常低效的,你也可以在'product.num'中找到'CP-%'和product.num,而不是'CP-I%''。 –

+0

@MarkRotteveel这是我尝试的第一件事。由于某种原因,我收到了错误信息。 –