copy first cell to second then add hours as int (DATEPART)
-
13-07-2021 - |
Domanda
I have data in the table like the following.
TimeIn TimeOut
-------------------------------------
6/1/2010 09:00:00 Null
6/2/2010 09:00:00 6/2/2010 16:45:00
6/3/2010 10:05:00 Null
6/4/2010 07:30:00 6/4/2010 15:45:00
i have the stored procedure to find last activity with "not signd out" column
i have a stored procedure for copy a cell to other too ...
then, what i need, is to update [TimeOut] (if there's no time out)
via more elegant way like
UPDATE TimeOut SET DATEPART(HOUR, TimeOut) = DATEPART(HOUR, TimeIN) + 8
so the Whole idea was to first check if last activity - Timeout Column is null
then if it is, sign TimeOut with max work hours allowed (8).
is there a simple way to do it ?
UPDATE
as to marc answer , this is the selection of find out if user didn't sign out
SELECT CASE WHEN [TimeOut] IS NULL THEN '' ELSE CONVERT(NVARCHAR,[TimeOut]) END FROM tblTime WHERE tId = ( SELECT MAX(tId) FROM tblTime WHERE UserId = 123
so i have the query that finds who did not sign out at last activity
then i only need to update that specific Row - field TimeOut
with hours of time in + 8 that was my question
Soluzione
Looks like you should use dateadd
.
UPDATE TimeOut
SET TimeOut = DATEADD(HOUR, 8, TimeIN)
WHERE ....
This will set TimeOut
to TimeIn
plus eight hours.
Altri suggerimenti
add a where TimeOut IS NULL
clause so it updates only the ones where the field actually is null?
UPDATE tblTime SET TimeOut = DATEADD(HOUR,8,TimeIn) WHERE tId = ( SELECT MAX(tId) FROM tblTime WHERE UserId = 1234)