以小数/百分比形式返回 datediff
我目前正在编写一个标量值函数,但返回的结果存在一些问题。
我已将问题范围缩小到将两个日期之间的差异转换为百分比/小数的计算。无论我尝试什么,返回值始终是一个整数
set @earnedpremium = (@premium * @pretripearnings) + ((@premium - (@premium * @pretripearnings)) * cast((datediff(day, @ outdate, @experiencedate) / datediff(day, @outdate, @returndate))asdecimal(5,2)))
强制转换部分需要返回百分比,我知道其余部分通过一些消除工作正常并且测试。
有人可以帮我弄清楚我做错了什么吗?
i am currently writing a scalar valued function and i'm having a few issue with the returned result.
i have narrowed the problem down to a calulation that convert the difference between two dates as a percentage/decimal. no matter what i try the return value is always a whole number
set @earnedpremium = (@premium * @pretripearnings) + ((@premium - (@premium * @pretripearnings)) * cast((datediff(day, @outdate, @experiencedate) / datediff(day, @outdate, @returndate))as decimal(5,2)))
the cast section needs to return the percentage, i know the rest is working fine through some elimination and testing.
can someone please help me figure out what im doing wrong??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 DATEDIFF 返回一个 INTEGER,因此您需要将该操作的两个部分都转换为 DECIMAL:
It's because DATEDIFF returns an INTEGER and so you need to cast both parts of that operation to DECIMAL: