Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 时间差_Sql_Sql Server_Tsql_Datediff - Fatal编程技术网

Sql 时间差

Sql 时间差,sql,sql-server,tsql,datediff,Sql,Sql Server,Tsql,Datediff,我的数据如下所示: Col1 Col2 output 09:35 16:00 6,25 <-- need help with this 给我7个 如果可能,我不想使用任何参数,只使用一些简单的函数。您可以尝试以下解决方案之一: -- Solution 1: Mathematical correct time CREATE TABLE #time(col1 time, col2 time) INSERT INTO #time(col1, col2) VALUE

我的数据如下所示:

Col1     Col2     output
09:35    16:00    6,25 <-- need help with this
给我7个


如果可能,我不想使用任何参数,只使用一些简单的函数。

您可以尝试以下解决方案之一:

-- Solution 1: Mathematical correct time
CREATE TABLE #time(col1 time, col2 time)

INSERT INTO #time(col1, col2)
VALUES(N'09:35',N'16:00'),(N'8:10',N'22:44')

SELECT col1, col2, CONVERT(decimal(10,2),DATEDIFF(MINUTE,Col1,Col2))/60  as [output]
FROM #time

DROP TABLE #time
GO

-- Solution 2: Your expected value
CREATE TABLE #time(col1 time, col2 time)

INSERT INTO #time(col1, col2)
VALUES(N'09:35',N'16:00'),(N'8:10',N'22:44')

SELECT DATEDIFF(MINUTE,Col1,Col2)/60 as [hours], DATEDIFF(MINUTE,Col1,Col2)%60 as [minutes],
    -- Contated values:
    DATEDIFF(MINUTE,Col1,Col2)/60 + (CONVERT(decimal(10,2),DATEDIFF(MINUTE,Col1,Col2)%60))/100 as [output]
FROM #time

DROP TABLE #time
解决方案1的输出

col1             col2             output
---------------- ---------------- ---------------------------------------
09:35:00.0000000 16:00:00.0000000 6.416666
08:10:00.0000000 22:44:00.0000000 14.566666
解决方案2的输出

hours       minutes     output
----------- ----------- ---------------------------------------
6           25          6.250000
14          34          14.340000

如果需要,您仍然可以舍入/转换值以匹配您的2位数模式。

您可以尝试以下解决方案之一:

-- Solution 1: Mathematical correct time
CREATE TABLE #time(col1 time, col2 time)

INSERT INTO #time(col1, col2)
VALUES(N'09:35',N'16:00'),(N'8:10',N'22:44')

SELECT col1, col2, CONVERT(decimal(10,2),DATEDIFF(MINUTE,Col1,Col2))/60  as [output]
FROM #time

DROP TABLE #time
GO

-- Solution 2: Your expected value
CREATE TABLE #time(col1 time, col2 time)

INSERT INTO #time(col1, col2)
VALUES(N'09:35',N'16:00'),(N'8:10',N'22:44')

SELECT DATEDIFF(MINUTE,Col1,Col2)/60 as [hours], DATEDIFF(MINUTE,Col1,Col2)%60 as [minutes],
    -- Contated values:
    DATEDIFF(MINUTE,Col1,Col2)/60 + (CONVERT(decimal(10,2),DATEDIFF(MINUTE,Col1,Col2)%60))/100 as [output]
FROM #time

DROP TABLE #time
解决方案1的输出

col1             col2             output
---------------- ---------------- ---------------------------------------
09:35:00.0000000 16:00:00.0000000 6.416666
08:10:00.0000000 22:44:00.0000000 14.566666
解决方案2的输出

hours       minutes     output
----------- ----------- ---------------------------------------
6           25          6.250000
14          34          14.340000

如果需要,您仍然可以舍入/转换值以匹配您的两位数模式。

我想我只需要明确地这样做,以分钟为单位计算差值并进行数值计算:

select (cast(datediff(minute, col1, col2) / 60 as varchar(255)) + ',' +
        right('00' + cast(datediff(minute, col1, col2) % 60 as varchar(255)), 2)
       )

我想我应该明确地做到这一点,以分钟为单位计算差值,然后进行数值计算:

select (cast(datediff(minute, col1, col2) / 60 as varchar(255)) + ',' +
        right('00' + cast(datediff(minute, col1, col2) % 60 as varchar(255)), 2)
       )

获取以分钟为单位的日期差异并将结果转换为所需字符串如何:

SELECT CONCAT(DATEDIFF(MINUTE, '09:35', '16:00') / 60, ':', DATEDIFF(MINUTE, '09:35', '16:00') % 60 );

获取以分钟为单位的日期差异并将结果转换为所需字符串如何:

SELECT CONCAT(DATEDIFF(MINUTE, '09:35', '16:00') / 60, ':', DATEDIFF(MINUTE, '09:35', '16:00') % 60 );

请注意,如果第二个col1时间大于col2时间,您将得到一个非常有趣的结果

通过简单地将这两个时间强制转换为datetime,您可以减去它们:

SELECT 
  cast(cast('16:00' as datetime) - cast('09:35' as datetime) as time(0))
结果:

06:25:00
06,25
如果您使用类似的格式(我更喜欢时间格式):

结果:

06:25:00
06,25

请注意,如果第二个col1时间大于col2时间,您将得到一个非常有趣的结果

通过简单地将这两个时间强制转换为datetime,您可以减去它们:

SELECT 
  cast(cast('16:00' as datetime) - cast('09:35' as datetime) as time(0))
结果:

06:25:00
06,25
如果您使用类似的格式(我更喜欢时间格式):

结果:

06:25:00
06,25

你为什么想要这样的格式?时间格式不是更好吗?你试图从一个时间中减去一个时间来获得一个新的时间,然后使用该格式而不是保持时间格式。你为什么想要这样的格式?时间格式不是更好吗?您尝试从时间中减去一个时间以获得新时间,然后使用该格式进行操作,而不是保持时间格式。