以小数/百分比形式返回 datediff

发布于 2024-10-09 22:57:41 字数 390 浏览 0 评论 0原文

我目前正在编写一个标量值函数,但返回的结果存在一些问题。

我已将问题范围缩小到将两个日期之间的差异转换为百分比/小数的计算。无论我尝试什么,返回值始终是一个整数

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2024-10-16 22:57:41

这是因为 DATEDIFF 返回一个 INTEGER,因此您需要将该操作的两个部分都转换为 DECIMAL:

set @earnedpremium = (@premium * @pretripearnings) + ((@premium - (@premium * @pretripearnings)) 
* (CAST(datediff(day, @outdate, @experiencedate) AS DECIMAL(5,2)) / 
  CAST(datediff(day, @outdate, @returndate) AS DECIMAL(5,2)))

It's because DATEDIFF returns an INTEGER and so you need to cast both parts of that operation to DECIMAL:

set @earnedpremium = (@premium * @pretripearnings) + ((@premium - (@premium * @pretripearnings)) 
* (CAST(datediff(day, @outdate, @experiencedate) AS DECIMAL(5,2)) / 
  CAST(datediff(day, @outdate, @returndate) AS DECIMAL(5,2)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文