Oracle SQL TO_CHAR变量长度

时间:2016-07-26 12:12:45

标签: sql oracle formatting to-char

我想在Oracle SQL中帮助您使用TO_CHAR方法。

TO_CHAR(Number_parameter,'9999D99','NLS_NUMERIC_CHARACTERS = ''. ''') vc_num,

Number_Parameter以十进制数形式出现,通常值介于10.10到1999.99之间(但不限于此)我需要创建一个VARCHAR,其值的最小值最多为2个小数点。我的问题是我没有找到满足我需要的面具。

我需要200.99作为'200.99',而10.1作为'10.1',而现在我得到' 10.1 '(带有空格,我不想要),150也应转换为{{1而不是'150'

你能帮我吗?

1 个答案:

答案 0 :(得分:2)

正如@a_horse_with_no_name建议的那样,使用FM format modifier将删除前导空格,并且还将删除尾随零。但它并不完全存在:

with t(number_parameter) as (
  select 200.99 from dual
  union all select 10.10 from dual
  union all select 150.00 from dual
)
select to_char(number_parameter,'FM9999D99','NLS_NUMERIC_CHARACTERS = ''. ''') vc_num
from t;

VC_NUM 
--------
200.99  
10.1    
150.   

要摆脱尾随时间,你需要修剪它:

with t(number_parameter) as (
  select 200.99 from dual
  union all select 10.10 from dual
  union all select 150.00 from dual
)
select rtrim(
  to_char(number_parameter,'FM9999D99','NLS_NUMERIC_CHARACTERS = ''. '''),
  '.') vc_num
from t;

VC_NUM 
--------
200.99  
10.1    
150

或者您发现自己,the TM text minimum format model也适用于您的数据:

with t(number_parameter) as (
  select 200.99 from dual
  union all select 10.10 from dual
  union all select 150.00 from dual
)
select to_char(number_parameter,'TM') vc_num
from t;

VC_NUM                                                         
----------------------------------------------------------------
200.99                                                          
10.1                                                            
150