找到sql中2次类型char之间的区别

时间:2012-08-10 11:28:48

标签: sql sql-server

我有两次char start_timeend_time。如何找到它们之间的区别?

例如我的start_time是上午10:00而end_time是上午11:15,那么时差应显示为1:15

5 个答案:

答案 0 :(得分:1)

试试这个:

declare @starttime varchar(10)='10:00 AM'
declare @endtime varchar(10)='11:15 AM'
select cast((datediff(Mi,convert (time,@starttime),
            convert (time,@endtime))/60) as varchar(5))+':'+
       cast((datediff(Mi,convert (time,@starttime),
              convert (time,@endtime))%60) as varchar(5))

结果

1:15

答案 1 :(得分:0)

这会让你在几分钟内得到不同的结果:

select DATEDIFF (minute, starttime, endtime)

请参阅DATEDIFF

答案 2 :(得分:0)

试试这个:

SELECT left(CONVERT(varchar(12), DATEADD(ms, DATEDIFF
(ms, CAST('10:00 AM' AS DATETIME), CAST('11:15 AM' AS DATETIME)) , 0), 108),5);

<强>结果

  

1时15

SELECT left(CONVERT(varchar(12), DATEADD(ms, DATEDIFF
(ms, CAST('09:00 AM' AS DATETIME), CAST('11:15 AM' AS DATETIME)) , 0), 108),5);

<强>结果

  

02:15

See this fiddle

答案 3 :(得分:0)

你可以这样做:

SELECT LEFT(convert(varchar,CAST('11:15 AM AS DATETIME)-CAST('10:00 AM' AS DATETIME),108),5)

答案 4 :(得分:0)

尝试此查询,它在MySQL中运行良好:

select TIME_FORMAT((TIME( STR_TO_DATE( '11:15 PM', '%h:%i %p' ) ) - TIME( STR_TO_DATE( '10:00 PM', '%h:%i %p' ))),'%h:%i');