提取天/小时/分钟

问题描述 投票:1回答:3

我有以下的代码工作,但我如何得到终端分钟,可以输出为日,小时,分钟?如果不能这样做,是有可能在这将表明它是第二天的时间添加+1?

我遇到的问题是,当我们的订单跑过去23.59 PM,该系统不显示,因为24小时时间段的正确格式。

我很为难,希望我没有混淆的问题。

SELECT FOLIO_NUMBER, TERMINAL_NAME,
  format((START_LOAD_TIME - ORDER_ENTRY_TIME), 'HH:mm') AS STAGING_MINUTES, 
  format((TERM_END_LOAD_TIME - START_LOAD_TIME), 'HH:mm') AS LOADING_MINUTES,
  format((TERM_END_LOAD_TIME - ORDER_ENTRY_TIME), 'HH:mm') AS TERMINAL_MINUTES 
FROM ORDERS  
JOIN TERMINAL_OWNER ON ORDERS.LOADING_TERMINAL_ID = TERMINAL_OWNER.TERMINAL_ID
sql sql-server date
3个回答
1
投票
DECLARE @INT INT

SET @INT = DATEDIFF(SECOND,GETDATE(),GETDATE()+1)

select 
convert(varchar(10), (@INT/86400)) + ':' + 
convert(varchar(10), ((@INT%86400)/3600)) + ':'+
convert(varchar(10), (((@INT%86400)%3600)/60)) + ':'+
convert(varchar(10), (((@INT%86400)%3600)%60)) as 'DD:HH:MM:SS'

NAT-MS提供。见here


1
投票

我认为你正在寻找这样的事情

Declare @theMinutes Varchar(10)
Set @theMinutes = '19:25' 

declare @totMintute int
Select    
  @totMintute = (Cast(
  Cast(left(@theMinutes,charindex(':',@theMinutes)-1) as Int) * 60
  + Cast(substring(@theMinutes,charindex(',',@theMinutes)+4,len(@theMinutes)) as Int)  
as Int ) * 60) / 60

--For 12 hour 1 days
Select @totMintute / 720 as NoDays  -- 720 minutes per day 
       , (@totMintute % 720) / 60 as NoHours -- modulo 720 
       , (@totMintute % 60) as NoMinutes -- modulo 60

--For 24 hour 1 days
Select @totMintute / 1440 as NoDays  -- 1440 minutes per day 
       , (@totMintute % 1440) / 60 as NoHours -- modulo 1440 
       , (@totMintute % 60) as NoMinutes -- modulo 60

输出将类似于如下图所示。

enter image description here

你可以把这个查询数据源表,如下图所示。

Create table #Temp (MinValue Varchar(8))
insert into #Temp Values ('19:25')

Select TotMinute / 720 as NoDays  -- 1440 minutes per day 
       , (TotMinute % 720) / 60 as NoHours -- modulo 1440 
       , (TotMinute % 60) as NoMinutes -- modulo 60
from(
select 
(Cast(
  Cast(left(MinValue,charindex(':',MinValue)-1) as Int) * 60
  + Cast(substring(MinValue,charindex(',',MinValue)+4,len(MinValue)) as Int)
as Int ) * 60) / 60  as TotMinute
from #Temp
)a

您可以在现场演示here


1
投票

我要说的是,你应该删除,需要确定在SQL输出天/小时,刚刚获得在几分钟内,你可以在你的应用程序层,然后上班的差异。

就拿this sample code

create table #orders (
    FOLIO_NUMBER int,
    START_LOAD_TIME datetime,
    ORDER_ENTRY_TIME datetime,
    TERM_END_LOAD_TIME datetime
)

insert into #orders (FOLIO_NUMBER,START_LOAD_TIME,ORDER_ENTRY_TIME,TERM_END_LOAD_TIME)
values (1, getdate(),getdate() - 1,getdate() + 1)

select *, 
    datediff(mi, ORDER_ENTRY_TIME, START_LOAD_TIME) AS STAGING_MINUTES,
    datediff(mi, START_LOAD_TIME, TERM_END_LOAD_TIME) AS LOADING_MINUTES,
    datediff(mi, ORDER_ENTRY_TIME, TERM_END_LOAD_TIME) AS TERMINAL_MINUTES
from #orders

drop table #orders

这将输出的事件之间的差别分钟:

FOLIO_NUMBER    STAGING_MINUTES LOADING_MINUTES TERMINAL_MINUTES
1               1440            1440            2880

然后,您可以使用这些值执行一些简单的数学提取,日,小时和分钟。

© www.soinside.com 2019 - 2024. All rights reserved.