请教cast和convert的用法,类型转换(数字-->字符)

xiaoyuehen 2003-10-14 04:41:58
我现在数如:

123456.9

23456.832

想最后输出

123,456.90

23,456.83
(即保留两位小数,不够位则补0,还要分段)
(VBS中有一函数可以实现这个功能,但我想在sql执行时就做好了)
...全文
329 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
DigJim 2003-10-14
  • 打赏
  • 举报
回复
为了考验我自己,我自己写了一个Function,不知道能不能帮你!
大力说的方法应该是可以的!!

create function dbo.f_ConvNum(
@dInputNum float
)
returns varchar(100)
as
begin
declare @OutPutNum varchar(100)
declare @strTemp varchar(100)

set @strTemp=cast(cast(@dInputNum as decimal(20,2)) as varchar(100))
set @OutPutNum=right(@strTemp,3)
set @strTemp=left(@strTemp,len(@strTemp)-3)


while(len(@strTemp)>3)
begin
set @OutPutNum=','+ right(@strTemp,3)+@OutPutNum
set @strTemp=left(@strTemp,len(@strTemp)-3)
end

set @OutPutNum=@strTemp+@OutPutNum

return @OutPutNum
end

----------------------------
调用:
select dbo.f_ConvNum(3144566123.3)
zjcxc 元老 2003-10-14
  • 打赏
  • 举报
回复

在下表中,左列表示从 money 或 smallmoney 转换为字符数据时的 style 值。

值 输出
0(默认值) 小数点左侧每三位数字之间不以逗号分隔,小数点右侧取两位数,例如 4235.98。
1 小数点左侧每三位数字之间以逗号分隔,小数点右侧取两位数,例如 3,510.92。
2 小数点左侧每三位数字之间不以逗号分隔,小数点右侧取四位数,例如 4235.9819。
xiaoyuehen 2003-10-14
  • 打赏
  • 举报
回复
不行呀...我用asp中的rs("字段")输出时就变成了1235.8了..而不是1,235.80
zjcxc 元老 2003-10-14
  • 打赏
  • 举报
回复
用convert

例如:
declare @a money
set @a=123456.9
select convert(varchar,@a,1)

pengdali 2003-10-14
  • 打赏
  • 举报
回复
declare @n money
set @n = 23456.832
select convert(varchar(100),cast(cast(@n as numeric(10,2)) as money),1)
yujohny 2003-10-14
  • 打赏
  • 举报
回复
declare @i float
set @i=1235.8
select cast(@i as decimal(12,2))

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧