gpt4 book ai didi

sql - 如何始终在 PL/SQL 中的数字上显示两位小数

转载 作者:行者123 更新时间:2023-12-04 23:19:30 25 4
gpt4 key购买 nike

伙计们,

我有一个现有的存储过程,我正在尝试更新它以始终显示两位小数,即使它是整数或单个小数。

此存储过程构建了一条必须显示 v_credit_amt 的消息。作为一个两位十进制数,但分配给 v_credit_amt 的值可以是整数或一位小数或两位小数
即 175 应显示为 175.00,250.5 应显示为 250.50,395.95 应显示为 395.95。

这是相关的股票pl/sql

v_credit_amt        number(22,2);
select NVL(sit_credit_limit,0)
into v_credit_amt
from sites
where sit_pkey = p_site_id;

我认为在选择期间通过 to_char 格式化可以解决这个问题,ala
select NVL(to_char(sit_credit_limit, 'fm99999.00'),0)
into v_credit_amt
from sites
where sit_pkey = p_site_id;

--v_credit_amt := to_char(v_credit_amt, 'fm99999.00');
insert into SQL_TXT values (v_credit_amt);
commit;

从上面注释掉的行可以看出,一旦定义了 v_credit_amt 变量,我也尝试过

我也尝试过使用 to_number 函数
select NVL(to_number(sit_credit_limit, '99999.00'),0)
into v_credit_amt
from sites
where sit_pkey = p_site_id;

但是,如果存储在 sit_credit_limit 列中的值是没有小数的整数,或者像 250.5 v_credit_amt 这样的数字在查询我插入以进行调试的 SQL_TXT 表时,在所有情况下都显示相同。
SQL> select * from SQL_TXT;

COL1
--------------------------------------------------------------------------------
175
250.5

这个特定事件只是将多个消息部分连接成一个返回的长消息字符串,即
if p_message_id = '14' then
v_message := 'Comtrol Check In Message Posted';
v_string := v_string || '008' || lpad(length(v_fname || ' ' || v_lname), 3, '0') || v_fname || ' ' || v_lname;
v_string := v_string || '059' || lpad(1, 3, '0') || '0';
--v_string := v_string || '089' || lpad(1, 3, v_credit_amt) || to_char(v_credit_amt);
v_string := v_string || '089' || lpad(length(to_char(v_credit_amt, 'fm9999999.00')), 3, '0') || to_char(v_credit_amt, 'fm9999999.00');
v_string := v_string || '106' || lpad(length(nvl(v_acct_number,'')), 3, '0') || v_acct_number;
--v_string := v_string || '106' || v_acct_number;
v_string := v_string || '164' || lpad(1, 3, '0') || '0';
v_string := v_string || '174' || lpad(length(v_rm_phone_num), 3, '0') || v_rm_phone_num;
v_string := v_string || '175' || lpad(length(v_rm_id), 3, '0') || v_rm_id;
v_string := v_string || '183' || lpad(1, 3, '0') || '0';
endif;

但是,我无法让最终字符串在所有用例中都正确包含两位小数。

例如,如果 db 中的值为 125,我会得到这个作为最终字符串
144450034629999008009Bob Smith05900100{89006125}1060073307542164001017400340917500
34091830010

然而它应该是
144450034629999008009Bob Smith05900100{89007125.00}1060073307542164001017400340917500
34091830010

抱歉上面的格式,我看不到如何在没有代码块的情况下加粗一个部分,所以我突出显示了相关部分而不是 {}

如果我需要始终将一个数字显示为两位小数,即使给出了一个整数或一位小数,我错过了什么?

最佳答案

实际上,您必须插入带有格式的数据。 (假设目标列是 VARCHAR )您获取并放入 NUMBER 的格式是什么变量,将是 NUMBER .

毕竟,数字没有要保存的格式。仅用于显示,格式进入图片。

insert into SQL_TXT values (TO_CHAR(v_credit_amt,'FM99999.00'));
commit;

如果 INSERT 列是 NUMBER再次。

你还想一起去
SELECT TO_CHAR(COL1,'FM99999.00') FROM SQL_TEXT;

关于sql - 如何始终在 PL/SQL 中的数字上显示两位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com