减去时间价值问题?

时间:2009-10-02 15:15:20

标签: sql sql-server sql-server-2000

使用SQL Server 2000

查询

Select id, CONVERT(char(8), CASE WHEN DateAdd(Day, - DateDiff(Day, 0, OutTime), OutTime) > Normal_Outtime THEN Cast(Normal_Outtime AS datetime) ELSE DateAdd(Day, - DateDiff(Day, 0, OutTime), OutTime) END - CASE WHEN DateAdd(Day, - DateDiff(Day, 0, InTime), InTime) < Normal_Intime THEN Cast(Normal_Intime AS datetime) ELSE DateAdd(Day, - DateDiff(Day, 0, InTime), InTime) END - cast(totalLunchtime AS datetime), 8) ELSE '00:00:00' END AS WorkedTime
from table

从上面的查询中,做outtime - intime - totallunchtime

列数据类型为varchar

实施例

Id  |Intime   |Outtime  |totallunchtime

001 |09:00:00 |21:00:00 |01:00:00
002 |07:00:00 |07:30:00 |01:00:00
003 |00:00:00 |00:00:00 |01:00:00

WorkedTime表示(outtime - Intime - totalLunchtime)

像这样获得输出

id  |workedtime

001 |11:00:00
002 |23:30:00 
003 |23:00:00

只有总时间的问题,而减去00:00:00,它给23:00:00,而对于人物002给它23:30:00它应该只给00:30:00

预期输出

id  |workedtime

001 |11:00:00
002 |00:23:00
003 |00:00:00

等......,

需要查询帮助

1 个答案:

答案 0 :(得分:1)

这样的事情会起作用吗?

declare @a datetime
declare @b datetime
declare @c datetime

set @a = '09:00:00'
set @b = '17:00:00'
set @c = '01:00:00'

select @b - @a - case when datediff(n, @a, @b) < 60 then '00:00:00' else @c end

如果您需要更多,那么您需要澄清在什么条件下应该考虑totallunchtime列。