SQL SERVER有类似ORACLE里的TRUNC()函数的吗?

cn21up 2005-08-03 10:29:30
例如数值3.1415,取三位小数,不要四舍五入,即得到3.141。我知道ORACLE里TRUNC()函数可以截取,SQLSERVER里有没有什么函数或者函数的组合可以做到?
...全文
1970 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
user968 2010-06-24
  • 打赏
  • 举报
回复
select convert(decimal(18,3),floor(3.1415*1000)/1000) 突然看到这个帖子, 很久前的问题了,不过感觉这个语句可以. 贴来共享.
cywarson 2005-08-04
  • 打赏
  • 举报
回复
我想只能用截取方式,不然一般都是四舍五入的.用left函数.
cn21up 2005-08-04
  • 打赏
  • 举报
回复
您们给的办法都不能用在VFP 8.0的环境下,还是不能解决问题。
橘子香水 2005-08-04
  • 打赏
  • 举报
回复
这个最简单:
declare @a numeric
set @a=3.1415
select round(3.1415-0.0005,3)

分别把@a设置为:3.1410和3.1419和3.1420看。
cxmcxm 2005-08-03
  • 打赏
  • 举报
回复
自己写一函数实现,
正数用floor,负数用ceiling
create function trunc(@inputnum float,@num int)
returns float
as
begin
declare @result float
if @inputnum>=0
set @result=floor(@inputnum*power(10,@num))/power(10,@num)
else
set @result=ceiling(@inputnum*power(10,@num))/power(10,@num)
return @result
end
vivianfdlpw 2005-08-03
  • 打赏
  • 举报
回复
--取3为小数
select convert(decimal(12,3),round(3.1415,3))

--结果
/*
--------------
3.142

(1 row(s) affected)
*/
tangqijun199 2005-08-03
  • 打赏
  • 举报
回复
我没用过那你还可以试试
先把数字转字符,再去小数点的位置,再用left
select left('3.1415',charindex('.','3.1415')+3)
cn21up 2005-08-03
  • 打赏
  • 举报
回复
我是在VFP8.0下用的。试过不支持numeric()
tangqijun199 2005-08-03
  • 打赏
  • 举报
回复
可以保留4位小数再用left取

select left(convert(numeric(10,4),3.1415),len(convert(numeric(10,4),3.1415))-1)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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