数据库把字段设计成了decimal,所以每一个存进去的字段都有五个有效数字~ 如:50000.00000
我之前的任务是把四个字段中的两个数字,两个条件,拼成 0<X<=100 的样子,如果直接拼出来的话,所有的数字都有五个有效小数,SQL语句如下:
select
FactorName,RS.FactorID,
RS.RewardStandardID,RS.RuleTemplateID,
case ABS (ConditionLowerBound) when 999999999 then '' else convert ( varchar , ConditionLowerBound) +
case convert ( varchar , ConditionLowerBoundContain) when 0 then ' < ' when 1 then ' <= ' end
end + ' X ' + case ABS (ConditionUpperBound) when 999999999 then '' else +
case convert ( varchar , ConditionUpperBoundContain) when 0 then ' < ' when 1 then ' <= ' end +
convert ( varchar , ConditionUpperBound) end AS Expression,StandardValue
from RewardStandard RS left join RewardComputingFactor RF on RS.FactorID = RF.FactorID
where 1 = 1
RS.RewardStandardID,RS.RuleTemplateID,
case ABS (ConditionLowerBound) when 999999999 then '' else convert ( varchar , ConditionLowerBound) +
case convert ( varchar , ConditionLowerBoundContain) when 0 then ' < ' when 1 then ' <= ' end
end + ' X ' + case ABS (ConditionUpperBound) when 999999999 then '' else +
case convert ( varchar , ConditionUpperBoundContain) when 0 then ' < ' when 1 then ' <= ' end +
convert ( varchar , ConditionUpperBound) end AS Expression,StandardValue
from RewardStandard RS left join RewardComputingFactor RF on RS.FactorID = RF.FactorID
where 1 = 1
输出如下:
明显,那表达式很难看~所以,现在的任务要把它变成正常的数字:
网上搜了一下,发现没有现成的答案,可能大学都觉得很简单,又或者我表述得不清楚吧。比如本标题,我也不知应该如何表述得更好。呵呵~
试过如下的方法:
select
right
(
1000
+
4555
,
3
)
select stuff ( convert ( varchar ,( 1000 + 4655 )), 1 , 1 , '' )
select replicate ( ' 0 ' , 3 - len ( convert ( varchar , 5 ))) + convert ( varchar , 5 )
select right ( 1000 + ConditionUpperBound, 10 ) from dbo.RewardStandard
select * from RewardStandard
select to_char( 12.1 , ' 99.00 ' ) from RewardStandard
declare @jj decimal ( 5 , 2 )
set @jj = 123.00
select convert ( varchar ( 30 ), @jj )
select CONVERT ( varchar , convert ( float ,ConditionUpperBound)) from dbo.RewardStandard
select stuff ( convert ( varchar ,( 1000 + 4655 )), 1 , 1 , '' )
select replicate ( ' 0 ' , 3 - len ( convert ( varchar , 5 ))) + convert ( varchar , 5 )
select right ( 1000 + ConditionUpperBound, 10 ) from dbo.RewardStandard
select * from RewardStandard
select to_char( 12.1 , ' 99.00 ' ) from RewardStandard
declare @jj decimal ( 5 , 2 )
set @jj = 123.00
select convert ( varchar ( 30 ), @jj )
select CONVERT ( varchar , convert ( float ,ConditionUpperBound)) from dbo.RewardStandard
发现有个方法取有个格式化后几位的好方法 如,5格式化为 005, select right(1000+5,3),呵呵,是不是很简单呢?
而我的问题呢,到现在还没有解决~最后,我无意中组合了一下,找到了办法。就是先把数字先转换成float再转换成varchar,如下:
select
CONVERT
(
varchar
,
convert
(
float
,ConditionUpperBound))
from
dbo.RewardStandard
所以,我的SQL就变成这个样子了~
select
FactorName,RS.FactorID,
RS.RewardStandardID,RS.RuleTemplateID,BillingMonth,RS.BaseTableID,RS.FactorID,
case ABS (ConditionLowerBound) when 999999999 then '' else convert ( varchar , convert ( float ,ConditionLowerBound)) +
case convert ( varchar , ConditionLowerBoundContain) when 0 then ' < ' when 1 then ' <= ' end
end + ' X ' + case ABS (ConditionUpperBound) when 999999999 then '' else +
case convert ( varchar , convert ( float ,ConditionUpperBoundContain)) when 0 then ' < ' when 1 then ' <= ' end +
convert ( varchar , convert ( float ,ConditionUpperBound)) end AS Expression, convert ( varchar , convert ( float ,StandardValue))
from RewardStandard RS left join RewardComputingFactor RF on RS.FactorID = RF.FactorID
where 1 = 1
RS.RewardStandardID,RS.RuleTemplateID,BillingMonth,RS.BaseTableID,RS.FactorID,
case ABS (ConditionLowerBound) when 999999999 then '' else convert ( varchar , convert ( float ,ConditionLowerBound)) +
case convert ( varchar , ConditionLowerBoundContain) when 0 then ' < ' when 1 then ' <= ' end
end + ' X ' + case ABS (ConditionUpperBound) when 999999999 then '' else +
case convert ( varchar , convert ( float ,ConditionUpperBoundContain)) when 0 then ' < ' when 1 then ' <= ' end +
convert ( varchar , convert ( float ,ConditionUpperBound)) end AS Expression, convert ( varchar , convert ( float ,StandardValue))
from RewardStandard RS left join RewardComputingFactor RF on RS.FactorID = RF.FactorID
where 1 = 1