Oracle - 数字到 varchar
我有一个包含 Number 类型列的表
create table tmp (
/*other fields*/
some_field Number
)
,在 PL SQL 脚本中,我想将该字段转换为 varchar。但是,我不知道它的长度,所以我得到了一个例外
异常消息是 ORA-06502: PL/SQL:数字或值错误: 字符串缓冲区太小
v_some_field varchar(21);
/*...*/
v_some_field := TO_CHAR(some_field,'999999999999999999999');
我应该如何声明v_some_field缓冲区?将其设置为 varchar(32767) 似乎很残酷,还有其他选择吗?
I have a table containing a column of type Number
create table tmp (
/*other fields*/
some_field Number
)
and in a PL SQL script, I want to convert that field to a varchar. However, i don't know its length, so I get an exception
Exception message is ORA-06502:
PL/SQL: numeric or value error:
character string buffer too small
v_some_field varchar(21);
/*...*/
v_some_field := TO_CHAR(some_field,'999999999999999999999');
How should i declare the v_some_field buffer? Setting it to varchar(32767) seems quite brute, is there any alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到错误不是因为数字太大,而是因为
to_char
的结果是 22 个字符长(21x"9"+ 一个符号字符):you're getting an error not because the number is too large but because the result of your
to_char
is 22 characters long (21x"9"+one character for the sign):您可以通过用整数和小数位转换负值来确定转换后的 varchar2 的最大长度:
对我来说,输出是 41。
You could determine the maximum length of your converted varchar2 by converting a negative value with integral and fractional digits:
Output is 41 for me.