2016-07-05 183 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這是我嘗試的第一件事。由於某種原因,我收到了錯誤信息。 –