Sql decode sign when length 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

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>長度函數是用來找出一個字串的長度。這個函數的名稱在不同的資料庫中不完全一樣:
  • MySQL: LENGTH( )
  • Oracle: LENGTH( )
  • SQL Server: LEN( )

案例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 中,其语法如下:
Sql decode sign when length concat 用法详述
Sql decode sign when length concat 用法详述SELECT <myColumnSpec> =
Sql decode sign when length concat 用法详述CASE
Sql decode sign when length concat 用法详述WHEN <A> THEN <somethingA>
Sql decode sign when length concat 用法详述WHEN <B> THEN <somethingB>
Sql decode sign when length concat 用法详述ELSE <somethingE>
Sql decode sign when length concat 用法详述END

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

























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

Sql decode sign when length concat 用法详述

上一篇:oracle 数据库开发面试题


下一篇:数据库日志文件收缩