如何获取sql server 2005中两个日期之间的月份数
我的 sql server 2005 表中有一个列应该保存员工的工作月数。
由于我还有员工的聘用日期,因此我希望“months_In_Service”列成为计算列。
现在,如果我使用 DATEDIFF(month,[DateEngged],GETDATE())
作为服务计算列中月份的公式,结果有时是正确的,有时是错误的。
获取 DateEngged 值与当前日期之间的月数的更可靠方法是什么?我应该在计算列中使用哪个公式?
I have a column in my sql server 2005 table that should hold the number of months an employee has been in service.
Since I also have the date the employee was engaged, I want the "months_In_Service" column to be a computed column.
Now if I use DATEDIFF(month,[DateEngaged],GETDATE())
as the formula for the months in service computed column, the results are correct some times and other times incorrect.
What would be the better reliable way to get the number of months between the DateEngaged value and the current date? Which formula should i use in my computed column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
类似于(可能需要交换 1 和 0,未经测试)
DATEDIFF 测量月份边界,例如每月 1 日的 00:00 时间,而不是每月的周年纪念日
编辑:看到 OP 的评论后,如果开始日>结束日
因此,对于 12 月 20 日至 1 月 13 日,DATEDIFF 给出 1,然后 20 > 13 因此减去 1 = 零个月。
Something like (might need to swap the 1 and 0, untested)
DATEDIFF measure month boundaries eg 00:00 time on 1st of each month, not day-of-month anniversaries
Edit: after seeing OP's comment, you have to subtract 1 if the start day > end day
So for 20 Dec to 13 Jan, DATEDIFF gives 1 and then 20 > 13 so subtract 1 = zero months.
与 gbn 相同的方法,但击键次数更少:-)
Same approach as gbn, but with less keystrokes :-)
也许你想要这样的东西:
Maybe you want something like:
如果您认为该月的含义为 30 天,您也可以将 vale 舍入
If You presume that month is meaning for 30 days You can also round vale