As a rule of thumb, iterative logic should be avoided in SQL. Wherever possible, the declarative nature of the language should be exploited - expressing your code as a set-based query.
I propose a solution like this or similar to this;
;WITH MyData (employeeID, employeeName, [Time])
AS
(
SELECT 1, 'Tim', '05/05/2014 08:15:42' UNION ALL
SELECT 1, 'Tim', '05/05/2014 16:30:51' UNION ALL
SELECT 1, 'Tim', '06/23/2014 07:00:00' UNION ALL
SELECT 1, 'Tim', '06/23/2014 09:00:00' UNION ALL
SELECT 1, 'Tim', '06/23/2014 11:00:00' UNION ALL
SELECT 1, 'Tim', '06/23/2014 16:30:00'
)
SELECT employeeID
,employeeName
,[Time In] = MIN([Time])
,[Time Out] = MAX([Time])
FROM MyData
GROUP BY employeeID, employeeName, CAST([Time] AS DATE)
If you are new to or uncomfortable with CTE
s;
CREATE TABLE #MyData
(
employeeID INT,
employeeName VARCHAR(50), -- Please use VARCHAR(MAX) as infrequently as possible
ClockTime DATETIME -- In this example, I've changed the field name from [Time] to ClockTime
) -- It is best to avoide reserved words for object\field naming
INSERT INTO #MyData (employeeID, employeeName, ClockTime)
SELECT 1, 'Tim', '05/05/2014 08:15:42' UNION ALL
SELECT 1, 'Tim', '05/05/2014 16:30:51' UNION ALL
SELECT 1, 'Tim', '06/23/2014 07:00:00' UNION ALL
SELECT 1, 'Tim', '06/23/2014 09:00:00' UNION ALL
SELECT 1, 'Tim', '06/23/2014 11:00:00' UNION ALL
SELECT 1, 'Tim', '06/23/2014 16:30:00'
SELECT employeeID
,employeeName
,[Time In] = MIN(ClockTime)
,[Time Out] = MAX(ClockTime)
FROM #MyData
GROUP BY employeeID, employeeName, CAST(ClockTime AS DATE)
Footnote;
I'm sure my sentiment will be echoed when I suggest that CURSORS
are the last thing you should ever try to learn in SQL. This will hopefully "force" you in to trying set-based approaches.