简体   繁体   English

在SQL中将datediff分钟转换为100种基本格式

[英]convert datediff minutes to 100 base format in sql

I want to calculate date time difference between two dates but the minutes should be in .100 scale ie if date time difference is 2.30 (2 Hours 30 Minutes) i want it in format 2.50 我想计算两个日期之间的日期时间差,但分钟数应为.100比例,即如果日期时间差为2.30 (2 Hours 30 Minutes)我希望它采用2.50格式

30 minutes = 0.50 30分钟= 0.50

i wrote a query for it but it does not work well when minutes are in range of 01 - 09 我为此写了一个查询,但当分钟数在01 - 09范围内时效果不佳

Case 1 : Wrong Output 情况1:输出错误

Declare @Start DateTime='02-03-2014 14:25:00'
Declare @End DateTime='02-03-2014 20:29:46'

Select STR(DateDiff(MINUTE,@Start,@End)/60)+'.'+STR(DateDiff(MINUTE,@Start,@End)%60/0.6)

DateTime Difference : 6.04 Expected Output : 6.10 Actual Output : 6.7 日期时间差: 6.04预期输出: 6.10实际输出: 6.7

Case 2 : Correct Output 情况2:正确的输出

Declare @Start DateTime='02-03-2014 13:55:02'
Declare @End DateTime='02-03-2014 17:33:31'

Select STR(DateDiff(MINUTE,@Start,@End)/60)+'.'+STR(DateDiff(MINUTE,@Start,@End)%60/0.6)

DateTime Difference : 3.38 Expected Output : 6.63 Actual Output : 6.63 DateTime差异: 3.38预期输出: 6.63实际输出: 6.63

what i am missing in case, when minutes are less than 10 ?? 如果分钟数少于10时,我会丢失什么?

DB : SQL Server 2008 数据库:SQL Server 2008

It seems in STR(DateDiff(MINUTE,@Start,@End)%60/0.6) 0.6 should be replaced with 6.0 STR(DateDiff(MINUTE,@Start,@End)%60/0.6)似乎应该用6.0代替

The above solution would round off upto 1 place after decimal. 上述解决方案将小数点后四舍五入至1位。

If you want to ROUND off to 2 places after decimal you can try the below snippet: 如果您想将小数点后ROUND到2位,可以尝试以下代码段:

SELECT CAST(CAST(ROUND(DateDiff(MINUTE,@Start,@End)%60/0.6) AS NUMERIC(12,2)) AS VARCHAR(50))

60 minutes = 60/60 = 1.0 60分钟= 60/60 = 1.0

30 minutes = 30/60 = 0.5 30分钟= 30/60 = 0.5

4 minutes = 4/60 = 0.066 , not 0.10 4分钟= 4/60 = 0.066 ,而不是0.10

0.10 = 6 minutes, not 4 minutes difference as in your first example. 0.10 = 6分钟,而不是第一个示例中的4分钟。

If we temporarily remove the seconds to reduce complexity, then you simply divide the seconds by 3600.00: 如果我们暂时删除秒以降低复杂性,则只需将秒除以3600.00:

Declare @Start DateTime='02-03-2014 14:25:00'
Declare @End DateTime='02-03-2014 20:29:00'

SELECT DATEDIFF(s,@Start,@End)/3600.00

This returns 6.066 which is the correct portion of hours to return 6 hours 4 minutes difference and a far simpler expression. 这将返回6.066 ,这是正确的小时数部分,以返回6小时4分钟的时差和更简单的表达式。

Try this, it should give a very exact result(after rounding): 试试这个,它应该给出非常精确的结果(四舍五入后):

Declare @Start DateTime='02-03-2014 13:55:02'
Declare @End DateTime='02-03-2014 17:33:31'

Select round(cast(@end-@start as float)*24, 2)

use this if you want to round down 如果要舍入,请使用此

Select floor(cast(@end-@start as float)*2400) / 100

My friend following is tabular presentation of you requirement. 我的朋友以下是您要求的表格演示。

1   1.666666667
2   3.333333333
3   5
4   6.666666667
5   8.333333333
6   10
7   11.66666667
8   13.33333333
9   15
10  16.66666667
11  18.33333333
12  20
13  21.66666667
14  23.33333333
15  25
16  26.66666667
17  28.33333333
18  30
19  31.66666667
20  33.33333333
21  35
22  36.66666667
23  38.33333333
24  40
25  41.66666667
26  43.33333333
27  45
28  46.66666667
29  48.33333333
30  50
31  51.66666667
32  53.33333333
33  55
34  56.66666667
35  58.33333333
36  60
37  61.66666667
38  63.33333333
39  65
40  66.66666667
41  68.33333333
42  70
43  71.66666667
44  73.33333333
45  75
46  76.66666667
47  78.33333333
48  80
49  81.66666667
50  83.33333333
51  85
52  86.66666667
53  88.33333333
54  90
55  91.66666667
56  93.33333333
57  95
58  96.66666667
59  98.33333333
60  100

In First Case you actual difference is 6 hours & 4 Minutes. 在第一种情况下,您的实际差值为6小时4分钟。 So as per requirement ans of 6.07 is correct how you are saying it is wrong? 因此,按照要求6.07 ans是正确的,您怎么说呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM