2017-04-13 88 views
0

如何修剪DB2中select查詢中列的前三個字符?我想下面的查詢,但沒有奏效修剪DB2中的前3個字符

SELECT 
MIN(Column1),UPPER(RIGHT(Column2, LENGTH(Column2 - 3))) AS TEST 
FROM TEST_TABLE 
GROUP BY UPPER(Column2) 
ORDER BY UPPER(Column2); 

我得到異常

The statement cannot be processed. 

User response: 

Correct the statement by including the expression in the GROUP BY clause 
that are in the SELECT clause, HAVING clause, or ORDER BY clause or by 
removing the column function from the SELECT statement. 

sqlcode: -119 

sqlstate: 42803 
+0

什麼意思',但沒有奏效'? – Jens

+0

@Jens我得到下面的異常....聲明無法處理。 用戶響應: 通過包括BY子句 是SELECT子句中的組的表達,HAVING子句或ORDER BY子句或通過 從SELECT語句除去列函數更正語句。 sqlcode:-119 sqlstate:42803 – Hari

回答

0

您可以簡單地使用字符串函數來完成下同。

SELECT 
MIN(Column1),substr(Column2,4) AS TEST 
FROM TEST_TABLE 
GROUP BY UPPER(substr(Column2,4)) 
ORDER BY UPPER(substr(Column2,4)); 
-1

只要做到:

SELECT 
MIN(Column1),UPPER(substr(Column2, 4)) AS TEST 
FROM TEST_TABLE 
GROUP BY UPPER(substr(Column2, 4)) 
ORDER BY 2;