Sql decode sign when length concat 用法详述

时间:2014-05-15 23:53:08   收藏:0   阅读:625

案例1:查询表A数据,如果某个列(PARAM_VALUE)值太长,前台不好显示,就只取前20个字符;鼠标悬浮时再用层显示全部值;

sql写法:
select
	m.PARAM_VALUE as PARAM_VALUE,
	decode(sign(length(m.PARAM_VALUE)-20),1,CONCAT(SUBSTR(m.PARAM_VALUE,0,20),'...'),m.PARAM_VALUE) as PARAM_VALUE_MSG
from tableA m

decode()函数简介:

主要作用:将查询结果翻译成其他值(即以其他形式表现出来,以下举例说明);

使用方法:

Select decode(columnname,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)

From talbename

Where …

        其中columnname为要选择的table中所定义的column

若只与一个值进行比较

Select monthid ,decode(sale, NULL,‘---’,sale) sale from output


sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1,


CONCAT() 的语法如下:

CONCAT(字串1, 字串2, 字串3,...): 将字串1、字串2、字串3,等字串连在一起。

请注意,Oracle的CONCAT()只允许两个参数;

换言之,一次只能将两个字串串连起来。不过,在Oracle中,我们可以用‘||‘来一次串连多个字串。


<strong><span style="font-size:18px;">length()</span></strong>長度函數是用來找出一個字串的長度。這個函數的名稱在不同的資料庫中不完全一樣:


案例2:某销售业务,付款方式Type为人民币时,返回人民币字段值,港币则返回港币值;

SELECT
   case b.ACTURAL_PAYMENT_CURRENCY when '2' then b.BUSINESS_FEE  when '1' then b.HK_BUSINESS_FEE end as "amount_of_money"
FROM tableB  b 

CASE
的语法:在一般的 SELECT 中,其语法如下:
bubuko.com,布布扣
bubuko.com,布布扣SELECT <myColumnSpec> =
bubuko.com,布布扣CASE
bubuko.com,布布扣WHEN <A> THEN <somethingA>
bubuko.com,布布扣WHEN <B> THEN <somethingB>
bubuko.com,布布扣ELSE <somethingE>
bubuko.com,布布扣END

这是 CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 GROUP BY 子句中的 CASE
bubuko.com,布布扣
bubuko.com,布布扣
SELECT Number of Titles, Count(*)
bubuko.com,布布扣
FROM titles
bubuko.com,布布扣
GROUP BY
bubuko.com,布布扣    
CASE
bubuko.com,布布扣        
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣        
WHEN price < 10 THEN Bargain
bubuko.com,布布扣        
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣        
ELSE Gift to impress relatives
bubuko.com,布布扣    
END
bubuko.com,布布扣
GO
bubuko.com,布布扣
bubuko.com,布布扣你甚至还可以组合这些选项,添加一个
ORDER BY 子句,如下所示:
bubuko.com,布布扣
bubuko.com,布布扣
USE pubs
bubuko.com,布布扣
GO
bubuko.com,布布扣
SELECT
bubuko.com,布布扣    
CASE
bubuko.com,布布扣        
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣        
WHEN price < 10 THEN Bargain
bubuko.com,布布扣        
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣        
ELSE Gift to impress relatives
bubuko.com,布布扣    
END AS Range,
bubuko.com,布布扣     Title
bubuko.com,布布扣
FROM titles
bubuko.com,布布扣
GROUP BY
bubuko.com,布布扣    
CASE
bubuko.com,布布扣        
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣        
WHEN price < 10 THEN Bargain
bubuko.com,布布扣        
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣        
ELSE Gift to impress relatives
bubuko.com,布布扣    
END,
bubuko.com,布布扣     Title
bubuko.com,布布扣
ORDER BY
bubuko.com,布布扣    
CASE
bubuko.com,布布扣        
WHEN price IS NULL THEN Unpriced
bubuko.com,布布扣        
WHEN price < 10 THEN Bargain
bubuko.com,布布扣        
WHEN price BETWEEN 10 and 20 THEN Average
bubuko.com,布布扣        
ELSE Gift to impress relatives
bubuko.com,布布扣    
END,
bubuko.com,布布扣     Title
bubuko.com,布布扣
GO
bubuko.com,布布扣
bubuko.com,布布扣注意,为了在
GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。

























Sql decode sign when length concat 用法详述,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!