SQL Server 格式化数值

来源:互联网 发布:高等数学c1网络课程 编辑:程序博客网 时间:2024/06/04 23:34

函数:

create   function   fn_FormatNumber(@Value   numeric(18,4))
returns   varchar(30)
as
begin
      declare   @t   varchar(30)
      set   @t=cast(@Value   as   varchar(30))
      while   right(@t,1)= '0'  
            set   @t=left(@t,len(@t)-1)


      declare   @i   int
      set   @i=charindex( '. ',@t)
      if   @i=0
set   @i=len(@t)
      else
set   @i=@i-1
      while   @i> 3
      begin
            set   @t=left(@t,@i-3)+ ', '+right(@t,len(@t)-@i+3)
            set   @i=@i-3
      end
      if   right(@t,1)= '. '
set   @t=left(@t,len(@t)-1)
      return     @t
end

调用:
select   dbo.fn_FormatNumber(123456789.1230)

原创粉丝点击