Sql 如果日期超过1天/24小时,则升起旗帜
我正在尝试创建一个列,当持续时间为从初始日期算起的一天(24小时或更长)时,该列将显示一个标志Sql 如果日期超过1天/24小时,则升起旗帜,sql,sql-server,datetime,case,date-arithmetic,Sql,Sql Server,Datetime,Case,Date Arithmetic,我正在尝试创建一个列,当持续时间为从初始日期算起的一天(24小时或更长)时,该列将显示一个标志 select case when (enddte - begindte) > '23:59:59.000' then '1' else '0' end as flag from shipment where ship_id = '14723' 需要注意的是: enddte和begindte的日期格式如下:2017-09-06 20:
select
case when (enddte - begindte) > '23:59:59.000'
then '1'
else '0'
end as flag
from shipment
where ship_id = '14723'
需要注意的是:
enddte和begindte的日期格式如下:2017-09-06 20:22:36.000
我是否需要进一步细分上面的案例陈述,计算白天和小时数?我相信,我已经解决了
select
case
when (datediff(hour,enddte, begindte)/24.0) > 1
then '1'
else '0'
end as flag
from shipment
where ship_id = '1326747-2'
我相信,我找到了答案
select
case
when (datediff(hour,enddte, begindte)/24.0) > 1
then '1'
else '0'
end as flag
from shipment
where ship_id = '1326747-2'
datediff()
不是执行此任务的好工具。每次跨越边界时,它都会增加1,这会产生与直觉相反的结果。通常,今天13:59和今天14:01之间的时差为1
您可以使用简单的日期算法,如下所示:
select s.*,
case when enddte > dateadd(day, 1, begindte) then 1 else 0 end as flag
from shipment s
where ship_id = 14723
当然,这假定这两列是合法的日期数据类型,例如datetime
或datetime2
datediff()
不是执行此任务的好工具。每次跨越边界时,它都会增加1,这会产生与直觉相反的结果。通常,今天13:59和今天14:01之间的时差为1
您可以使用简单的日期算法,如下所示:
select s.*,
case when enddte > dateadd(day, 1, begindte) then 1 else 0 end as flag
from shipment s
where ship_id = 14723
当然,这假设这两列是合法的日期数据类型,例如
datetime
或datetime2
请用正在运行的数据库标记您的问题:mysql、oracle、postgresql。。。?另外,列begindte
和enddte
的数据类型是什么?@GMB:看起来OP正在使用MSSQL。我相应地更新了标签。另外:我不确定OP是如何调用此查询的,但我不确定“选择case when”是最佳方法…@GMB,是的,我使用的是MSSQL。数据类型是datetime。我在想这个查询可能看起来像这样,但还没有开始工作。选择case when(datediff(hour,enddte,begindte)/24.0)>1,然后选择'1'或'0'作为装运标志结束,其中ship_id='14723'请使用正在运行的数据库标记您的问题:mysql、oracle、postgresql。。。?另外,列begindte
和enddte
的数据类型是什么?@GMB:看起来OP正在使用MSSQL。我相应地更新了标签。另外:我不确定OP是如何调用此查询的,但我不确定“选择case when”是最佳方法…@GMB,是的,我使用的是MSSQL。数据类型是datetime。我在想这个查询可能看起来像这样,但还没有开始工作。当(datediff(hour,enddte,begindte)/24.0)大于1时,选择case,然后选择'1'或'0'作为装运标志结束,其中ship_id='14723'