decode(
ul.premium_alloc,like '_%',to_number(replace(ul.premium_alloc, '%', ' ')),like '%[ %]',to_number(replace(ul.premium_alloc, ' %', ' ')),select premium_alloc from dw_ssa.ssa_ul_market_value where issuer_id='HK01371',to_number(ul.premium_alloc)*100,ul.premium_alloc
)
在decode里面,怎么写条件表达式?like什么呢?我想将所有以百分号结尾的数字替换成单一的数字,如:40%要替换成40;还有如:40 %要替换成40(注意,40后面有个空格);还有查询出数据为小数的记录,并替换成原来的数乘以100;我上面写的模糊查询条件是错的,还有那个SQL语句也不太正确,我加上rownum=1才对了,所以为达到要求,不能用SQL语句,请帮帮忙,感激不尽!!
6 个解决方案
#1
LZ列出这个sql干嘛呢?不知道啥表 不知道字段啥意思
直接举例几个数据 然后说出要求就可以了
直接举例几个数据 然后说出要求就可以了
#2
还有查询出数据为小数的记录,并替换成原来的数乘以100? 不大懂 还是列举数据容易理解点
#3
WITH t AS
( SELECT '30' AS rat FROM dual
UNION ALL
SELECT '40%' FROM dual
UNION ALL
SELECT '45 %' FROM dual
UNION ALL
SELECT '0.51' FROM dual
)
SELECT regexp_replace(rat,'[^0-9]','')
FROM
(SELECT
CASE
WHEN instr(rat,'.') > 0
THEN TO_CHAR(to_number(rat)*100)
ELSE rat
END as rat
FROM t
)
REGEXP_REPLACE(RAT,'[^0-9]','')
--------------------------------
30
40
45
51
( SELECT '30' AS rat FROM dual
UNION ALL
SELECT '40%' FROM dual
UNION ALL
SELECT '45 %' FROM dual
UNION ALL
SELECT '0.51' FROM dual
)
SELECT regexp_replace(rat,'[^0-9]','')
FROM
(SELECT
CASE
WHEN instr(rat,'.') > 0
THEN TO_CHAR(to_number(rat)*100)
ELSE rat
END as rat
FROM t
)
REGEXP_REPLACE(RAT,'[^0-9]','')
--------------------------------
30
40
45
51
#4
create table t1 (c1 number(5),c2 varchar2(10));
insert into t1 values (1,'50%');
insert into t1 values (2,'33 %');
insert into t1 values (3,'0.4');
insert into t1 values (4,'99%');
commit;
select decode(substr(c2,-1),'%',trim(substr(c2,1,length(c2)-1)),c2*100) c2
from t1
C2
--------------
1 50
2 33
3 40
4 99
#5
谢谢你们的热心回帖,你们的方法都是对的,其实还有种方法:
to_number(replace(varcharfield,'%',''))。
再次感谢!结贴了。
to_number(replace(varcharfield,'%',''))。
再次感谢!结贴了。
#6
结贴了么 赶紧的
#1
LZ列出这个sql干嘛呢?不知道啥表 不知道字段啥意思
直接举例几个数据 然后说出要求就可以了
直接举例几个数据 然后说出要求就可以了
#2
还有查询出数据为小数的记录,并替换成原来的数乘以100? 不大懂 还是列举数据容易理解点
#3
WITH t AS
( SELECT '30' AS rat FROM dual
UNION ALL
SELECT '40%' FROM dual
UNION ALL
SELECT '45 %' FROM dual
UNION ALL
SELECT '0.51' FROM dual
)
SELECT regexp_replace(rat,'[^0-9]','')
FROM
(SELECT
CASE
WHEN instr(rat,'.') > 0
THEN TO_CHAR(to_number(rat)*100)
ELSE rat
END as rat
FROM t
)
REGEXP_REPLACE(RAT,'[^0-9]','')
--------------------------------
30
40
45
51
( SELECT '30' AS rat FROM dual
UNION ALL
SELECT '40%' FROM dual
UNION ALL
SELECT '45 %' FROM dual
UNION ALL
SELECT '0.51' FROM dual
)
SELECT regexp_replace(rat,'[^0-9]','')
FROM
(SELECT
CASE
WHEN instr(rat,'.') > 0
THEN TO_CHAR(to_number(rat)*100)
ELSE rat
END as rat
FROM t
)
REGEXP_REPLACE(RAT,'[^0-9]','')
--------------------------------
30
40
45
51
#4
create table t1 (c1 number(5),c2 varchar2(10));
insert into t1 values (1,'50%');
insert into t1 values (2,'33 %');
insert into t1 values (3,'0.4');
insert into t1 values (4,'99%');
commit;
select decode(substr(c2,-1),'%',trim(substr(c2,1,length(c2)-1)),c2*100) c2
from t1
C2
--------------
1 50
2 33
3 40
4 99
#5
谢谢你们的热心回帖,你们的方法都是对的,其实还有种方法:
to_number(replace(varcharfield,'%',''))。
再次感谢!结贴了。
to_number(replace(varcharfield,'%',''))。
再次感谢!结贴了。
#6
结贴了么 赶紧的