I want to calculate days to month like below
我想像下面这样计算每天的数据
I am using this code to get months:
我使用这段代码来获得几个月:
DECLARE @Date datetime = '2017-12-06'
SELECT
CAST(DATEDIFF(M, @Date, GETDATE()) AS varchar) + ' Months and ' +
CAST(DATEDIFF(D, @Date, DATEADD(M, - DATEDIFF(M, @Date, GETDATE()), GETDATE())) AS varchar) + ' Days'
Output:
(Result)
3 Months and 4 Days
It works but I need output like this
它工作但我需要像这样的输出
90 days + 4 days = 94days
Expected output:
Month=3.09041
Thanks in advance
提前致谢
1 个解决方案
#1
1
On average, a month has 30.43 days (365.25 / 12). How about just doing this?
平均而言,一个月有30.43天(365.25 / 12)。这样做怎么样?
SELECT DATEDIFF(days, @Date, GETDATE()) / (365.25 / 12)
This does not produce your exact results but it is a very good estimate of decimal months.
这不会产生您的确切结果,但它是十进制月的非常好的估计。
#1
1
On average, a month has 30.43 days (365.25 / 12). How about just doing this?
平均而言,一个月有30.43天(365.25 / 12)。这样做怎么样?
SELECT DATEDIFF(days, @Date, GETDATE()) / (365.25 / 12)
This does not produce your exact results but it is a very good estimate of decimal months.
这不会产生您的确切结果,但它是十进制月的非常好的估计。