想要四舍五入的数据

时间:2018-07-19 09:33:01

标签: oracle

我的查询是:

SELECT TO_CHAR((to_date('01-01-2018 00:00:00','DD-MM-YYYY HH24:MI:SS')+ (level-1)),'DD-MM-YYYY'),
        TO_CHAR(to_date('01-01-2018 00:00:00','DD-MM-YYYY HH24:MI:SS')      + level,'DD-MM-YYYY') ,      
        to_number(regexp_substr('7000 T', '^\d+'))/(TO_DATE('04-01-2018 00:00:00', 'DD-MM-YYYY HH24:MI:SS') - TO_DATE('01-01-2018 00:00', 'DD-MM-YYYY HH24:MI:SS'))
        || regexp_substr('7000 T', '[A-Z]') AS IP_PLAN      
        FROM dual
        CONNECT BY level <= to_date('04-01-2018 00:00:00','DD-MM-YYYY HH24:MI:SS')-to_date('01-01-2018 00:00:00','DD-MM-YYYY HH24:MI:SS');

我想要IP_PLAN,例如:2333 T

2 个答案:

答案 0 :(得分:0)

ROUND(('7000 T'), '[A-Z]')试图舍入一个字符串值,即从该源字符串中提取的T,这没有任何意义。

您需要四舍五入之前生成的数字:

select to_char(to_date('01-01-2018 00:00:00', 'DD-MM-YYYY HH24:MI:SS') + level - 1,
         'DD-MM-YYYY'),
       to_char(to_date('01-01-2018 00:00:00', 'DD-MM-YYYY HH24:MI:SS') + level,
         'DD-MM-YYYY') ,      
       round(
         to_number(regexp_substr('7000 T', '^[[:digit:]]+'))
           / (to_date('04-01-2018 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
               - to_date('01-01-2018 00:00', 'DD-MM-YYYY HH24:MI:SS'))
       )
       || regexp_substr('7000 T', '[^[:digit:]]*$') as ip_plan_consumption      
from dual
connect by level <= to_date('04-01-2018 00:00:00', 'DD-MM-YYYY HH24:MI:SS')
    - to_date('01-01-2018 00:00:00', 'DD-MM-YYYY HH24:MI:SS');

TO_CHAR(TO TO_CHAR(TO IP_PLAN_CONSUMPTION                       
---------- ---------- ------------------------------------------
01-01-2018 02-01-2018 2333 T                                    
02-01-2018 03-01-2018 2333 T                                    
03-01-2018 04-01-2018 2333 T                                    

所以你在做

round(<number extracted by regex> / <difference in days >)

round(7000 / 3) => round(2333.333...) => 2333

,然后在病房后面附加T

我稍微修改了正则表达式模式,因此它在末尾占用了空格和任何字符。不过,这还是对该字符串中的可能值进行了一些假设-即,它始终是一个数字,然后是一个非数字部分。


顺便说一句,如果您使用固定日期,则使用日期文字更简单:

select to_char(date '2018-01-01' + level - 1, 'DD-MM-YYYY'),
       to_char(date '2018-01-01' + level, 'DD-MM-YYYY') ,      
       round(
         to_number(regexp_substr('7000 T', '^[[:digit:]]+'))
           / (date '2018-01-04' - date '2018-01-01')
       )
       || regexp_substr('7000 T', '[^[:digit:]]*$') as ip_plan_consumption      
from dual
connect by level <= date '2018-01-04' - date '2018-01-01';

尽管我想象7000 T和日期值都在您的真实代码中以字符串形式传递,所以需要对其进行转换。不过,您可以使用CTE仅将它们转换一次,而不是在循环时重复转换。

答案 1 :(得分:0)

SELECT TO_CHAR((to_date('01-01-2018 00:00:00','DD-MM-YYYY HH24:MI:SS')+ (level-1)),'DD-MM-YYYY'),
        TO_CHAR(to_date('01-01-2018 00:00:00','DD-MM-YYYY HH24:MI:SS')      + level,'DD-MM-YYYY') ,      
        round(to_number(regexp_substr('7000 T', '^\d+'))/(TO_DATE('04-01-2018 00:00:00', 'DD-MM-YYYY HH24:MI:SS') - TO_DATE('01-01-2018 00:00', 'DD-MM-YYYY HH24:MI:SS')),0)
         AS IP_PLAN_CONSUMPTION      
        FROM dual
        CONNECT BY level <= to_date('04-01-2018 00:00:00','DD-MM-YYYY HH24:MI:SS')-to_date('01-01-2018 00:00:00','DD-MM-YYYY HH24:MI:SS');

您必须在其他列中使用T;