String Oracle中的Sybase-STR函数
是否有一个等效于Sybase的STR函数,它将数字转换为字符串 在甲骨文?我找不到。如果没有,有没有一种优雅的方式来复制它 非常感谢 编辑:例如:String Oracle中的Sybase-STR函数,string,oracle,function,sybase,String,Oracle,Function,Sybase,是否有一个等效于Sybase的STR函数,它将数字转换为字符串 在甲骨文?我找不到。如果没有,有没有一种优雅的方式来复制它 非常感谢 编辑:例如: str(123.56,30,10) 在Sybase中,给出一个总长度为30的字符串,正好在小数点后10位,并在左侧填充空格,因此结果为 123.5600000000 或者,更一般地说(根据需要分别替换30和10): 注意:字符串长度将为31,以便为可能的“-”(负号)留出空间。由于oracle没有此类特殊功能,
str(123.56,30,10)
在Sybase中,给出一个总长度为30的字符串,正好在小数点后10位,并在左侧填充空格,因此结果为
123.5600000000
或者,更一般地说(根据需要分别替换30和10):
注意:字符串长度将为31,以便为可能的“-”(负号)留出空间。由于oracle没有此类特殊功能,您需要创建一个。所以,这里的这个会帮助你。也许它需要一些改进来检查len是否大于小数。但这是一个良好的开端
create or replace function str( num in number,
len in integer,
decsLength in integer ) return varchar2 is
decSep nls_database_parameters.value%type;
befSep varchar2(255);
aftSep varchar2(255);
finalStr varchar2(255);
begin
--this gets the decimal separator, It depends on the
--database configuration.
select substr(value, 2, 1) into decSep
from nls_database_parameters
where parameter = 'NLS_NUMERIC_CHARACTERS';
select substr( to_char(num), 1,
decode( instr(to_char(num), decSep),
0,
length(to_char(num)),
instr(to_char(num), decSep)-1) )
into befSep from dual;
select decode( instr(to_char(num), decSep),
0,
'',
substr(to_char(num),
instr(to_char(num),decSep)+1))
into aftSep from dual;
if instr(to_char(num),decSep) > 0 then
finalStr := lpad( befSep, len-(decsLength+1), ' ' )
|| decSep || rpad(aftSep,decsLength, '0');
else
finalStr := lpad(befSep, len, ' ' );
end if;
return finalStr;
end str;
我想您可以使用
来指定格式的字符。另外,在你的问题和期望的结果中添加一些数据。然后我就可以帮你完成to_char函数了。好的,谢谢。查看编辑后的文章pleasea_horse_with_no_name的解决方案似乎对我来说很好,而且非常简单。不过,感谢您向我展示了您是如何实现此功能的!我创建这个函数只是因为你要求一个类似的函数,当你改变参数时,这个函数在任何情况下都适用。我知道to_char函数,但它是用掩码修复的。
select to_char(123.56, lpad(rpad('.',10,'0'),30,'9'))
from dual;
create or replace function str( num in number,
len in integer,
decsLength in integer ) return varchar2 is
decSep nls_database_parameters.value%type;
befSep varchar2(255);
aftSep varchar2(255);
finalStr varchar2(255);
begin
--this gets the decimal separator, It depends on the
--database configuration.
select substr(value, 2, 1) into decSep
from nls_database_parameters
where parameter = 'NLS_NUMERIC_CHARACTERS';
select substr( to_char(num), 1,
decode( instr(to_char(num), decSep),
0,
length(to_char(num)),
instr(to_char(num), decSep)-1) )
into befSep from dual;
select decode( instr(to_char(num), decSep),
0,
'',
substr(to_char(num),
instr(to_char(num),decSep)+1))
into aftSep from dual;
if instr(to_char(num),decSep) > 0 then
finalStr := lpad( befSep, len-(decsLength+1), ' ' )
|| decSep || rpad(aftSep,decsLength, '0');
else
finalStr := lpad(befSep, len, ' ' );
end if;
return finalStr;
end str;