求SQL语句

hzybc 2010-04-26 09:55:37
SQL 中,有一列(L1)是Money

值为:12345.67

想要得到:12,346

求SQL语句
...全文
82 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2010-04-26
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 dawugui 的回复:]
SQL code
create table tb(l1 Money)
insert into tb values(12345.67)
insert into tb values(12334245.67)
insert into tb values(12334534545.67)

go

select convert(varchar , l1 , 1) from tb

dro……
[/Quote]楼主要去掉小数
dawugui 2010-04-26
  • 打赏
  • 举报
回复
create table tb(l1 Money)
insert into tb values(12345.67)
insert into tb values(12334245.67)
insert into tb values(12334534545.67)

go

select convert(varchar , l1 , 1) from tb

drop table tb

/*

------------------------------
12,345.67
12,334,245.67
12,334,534,545.67

(所影响的行数为 3 行)
*/
dawugui 2010-04-26
  • 打赏
  • 举报
回复
我怎么觉得九楼的最好呢?嘿嘿.
envykok 2010-04-26
  • 打赏
  • 举报
回复

select '$'+convert(varchar(256),CAST('1600000000' AS money),1)
htl258_Tony 2010-04-26
  • 打赏
  • 举报
回复
8#的不错,函数也是可以的。



create function fn_testnum(@m money)
returns varchar(20)
as
begin
declare @s varchar(20),@r varchar(20)
set @s=reverse(ltrim(cast(@m as dec(28,0)))) --这里去掉ceiling,我原以为是全舍入的需求
while len(@s)>0
select @r=isnull(@r+',','')+left(@s,3),
@s=stuff(@s,1,3,'')
return reverse(@r)
end
go

declare @m money
set @m=12345.67
select dbo.fn_testnum(@m)
/*
--------------------
12,346

(1 行受影响)
*/
set @m=1234566545.67
select dbo.fn_testnum(@m)
/*
--------------------
1,234,566,546

(1 行受影响)
*/

fengfeng86 2010-04-26
  • 打赏
  • 举报
回复
select convert(varchar,convert(money,12345.67),1)

--去掉小数位
select reverse(stuff(reverse(convert(varchar,convert(money,12345.67),1)),1,3,''))
dawugui 2010-04-26
  • 打赏
  • 举报
回复
create table tb(l1 Money)
insert into tb values(12345.67)
go

select convert(varchar , l1 , 1) from tb

drop table tb

/*


------------------------------
12,345.67

(所影响的行数为 1 行)
*/
csdyyr 2010-04-26
  • 打赏
  • 举报
回复
select replace(convert(varchar,convert(money,round(12345.67, 0)),1), '.00', '')
htl258_Tony 2010-04-26
  • 打赏
  • 举报
回复


create function fn_testnum(@m money)
returns varchar(20)
as
begin
declare @s varchar(20),@r varchar(20)
set @s=reverse(ltrim(ceiling(cast(@m as dec(28,4)))))
while len(@s)>0
select @r=isnull(@r+',','')+left(@s,3),
@s=stuff(@s,1,3,'')
return reverse(@r)
end
go

declare @m money
set @m=12345.67
select dbo.fn_testnum(@m)
/*
--------------------
12,346

(1 行受影响)
*/
set @m=1234566545.67
select dbo.fn_testnum(@m)
/*
--------------------
1,234,566,546

(1 行受影响)
*/
--小F-- 2010-04-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 csdyyr 的回复:]
SQL code
select convert(varchar,convert(money,round(12345.67, 0)),1)
[/Quote]

巧啊 不过多了两个0
csdyyr 2010-04-26
  • 打赏
  • 举报
回复
select convert(varchar,convert(money,round(12345.67, 0)),1)
dawugui 2010-04-26
  • 打赏
  • 举报
回复
利用conert()可以转换,你找找,我忘记了.
--小F-- 2010-04-26
  • 打赏
  • 举报
回复
可以写 但是没必要用SQL来写 万一要写也是可以的 上次有人发过这个函数
黄_瓜 2010-04-26
  • 打赏
  • 举报
回复
还是用程序来处理吧
bancxc 2010-04-26
  • 打赏
  • 举报
回复
没有大大回答 说明sql行不通
还是不要用sql做

34,590

社区成员

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

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