【问题标题】:PLSQL calculation and month_between from a table表中的 PL SQL 计算和months_between
【发布时间】:2016-05-15 17:03:43
【问题描述】:

我在创建匿名 PL/SQL 代码时遇到问题,该代码应从表中打印出姓名、姓氏和年龄,并带有 1 位小数(而不是 sysdate,我使用 01-SEP-04 ddmonyyyy)。我遇到的问题是年龄。到目前为止,这是我想出的:

set serveroutput on

 declare cursor
  car_owner is select firstN, lastN, trunc(months_between(date '2004-09-01', to_date('19' || substr(pnr, 1,6), 'yyyymmdd')))/12
from carowners;

b_first carowners.firstN%type;
b_last carowners.lastN%type;
age number (3,1);

begin
 if not car_owner%isopen
  then open car_owner;
 end if;

loop 
 fetch car_owner into b_first, b_last, age;

exit when car_owner%notfound;

dbms_output.put_line (initcap(b_first || ', ' || b_last) || ', ' || age ||' years old');

       end loop;
       close car_owner;

end;

我得到的输出是这样的:

马丁,瓦格,55.4 岁
肯尼·格林,50.6 岁
托尼,扎维克,39 岁
Shawon, Hassan, 33.3 岁
萨德克,诺维奇,35.4 岁
简,罗伯茨,45.3 岁
特里,麦克菲尔德,43.4 岁
Malice, Vanzi, 23.4 岁

我正在寻找的是这样的输出:

Martin,Varg,55.6 岁
肯尼,格林,50.7 岁
Tony, Zawicki, 39,1 岁
Shawon, Hassan, 33,4 岁
萨德克,诺维奇,35.6 岁
简,罗伯茨,45.5 岁
特里,麦克菲尔德,43.6 岁
Malice,Vanzi,23.5 岁

请注意,我需要一个逗号和正确的小数...

感谢所有建议和帮助!

【问题讨论】:

  • 你如何从 55.4 到 55.6 或 50.6 到 50.7?这不仅仅是小数点的替换。

标签: sql oracle plsql cursor


【解决方案1】:

您可以使用国际化将小数点设置为逗号。在文档中启动here

但是,对于一次性解决方案,我只需将 age 替换为:

trim(replace(to_char(age, '999.9'), '.', ','))

【讨论】:

  • 这会起作用,但首先你必须对年龄执行 ceil() 到小数点后的第一位。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多