如果字数超过20,则追加字符

时间:2014-04-30 09:45:27

标签: sql oracle

使用Oracle SQL,我想附加一个带字符的字符串 - 如果该字符串的大小超过20.我该怎么做?

我的尝试就是这个。

SELECT 
DECODE(PKG_DESC, 
LENGTH(PKG_DESC) > 20, '...',
'UNKNOWN'
) 
FROM VW_LOGIN_INF;

但显示错误

Error at Command Line:3 Column:22
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:

3 个答案:

答案 0 :(得分:1)

SELECT 
CASE WHEN LENGTH(sentence) > 50 THEN
     sentence || '...'
     WHEN LENGTH(sentence) > 20 THEN
     sentence || '.'
     ELSE
     sentence
END as sentence
FROM
YOURTABLE

答案 1 :(得分:1)

谢谢@OracleUser这充满了我的需求..

SELECT 
CASE WHEN LENGTH(PKG_DESC) > 50 THEN
     SUBSTR(PKG_DESC,0,INSTR(PKG_DESC,' ',1,10)) || '...'
     WHEN LENGTH(PKG_DESC) > 20 THEN
     SUBSTR(PKG_DESC,0,INSTR(PKG_DESC,' ',1,10)) || '.'
     ELSE
     PKG_DESC
END as PKG_DESC
FROM
VW_LOGIN_INF

答案 2 :(得分:0)

这样的东西?

 SELECT decode(sentence || appstr, length(sentence) > 20, sentence) FROM TABLE