标签:sql decode when sign concat
案例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
主要作用:将查询结果翻译成其他值(即以其他形式表现出来,以下举例说明);
使用方法:
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 的典型用法,但是使用
CASE 其实可以做更多的事情。比方说下面的
GROUP
BY 子句中的 CASE:
SELECT
‘Number of Titles‘,
Count(*)
FROM
titles
GROUP
BY
CASE
WHEN
price IS
NULL THEN
‘Unpriced‘
WHEN
price <
10
THEN
‘Bargain‘
WHEN
price BETWEEN
10
and
20
THEN
‘Average‘
ELSE
‘Gift to impress relatives‘
END
GO
你甚至还可以组合这些选项,添加一个
ORDER
BY 子句,如下所示:
USE
pubs
GO
SELECT
CASE
WHEN
price IS
NULL THEN
‘Unpriced‘
WHEN
price <
10
THEN
‘Bargain‘
WHEN
price BETWEEN
10
and
20
THEN
‘Average‘
ELSE
‘Gift to impress relatives‘
END
AS Range,
Title
FROM
titles
GROUP
BY
CASE
WHEN
price IS
NULL THEN
‘Unpriced‘
WHEN
price <
10
THEN
‘Bargain‘
WHEN
price BETWEEN
10
and
20
THEN
‘Average‘
ELSE
‘Gift to impress relatives‘
END,
Title
ORDER
BY
CASE
WHEN
price IS
NULL THEN
‘Unpriced‘
WHEN
price <
10
THEN
‘Bargain‘
WHEN
price BETWEEN
10
and
20
THEN
‘Average‘
ELSE
‘Gift to impress relatives‘
END,
Title
GO
注意,为了在
GROUP
BY 块中使用 CASE,查询语句需要在
GROUP
BY 块中重复 SELECT 块中的
CASE 块。
Sql decode sign when length concat 用法详述,布布扣,bubuko.com
Sql decode sign when length concat 用法详述
标签:sql decode when sign concat
原文地址:http://blog.csdn.net/yinyuehepijiu/article/details/25903343