Deepak M

Deepak M

  • 1.2k
  • 546
  • 37.3k

How to get In/Out log in SQL Server?

Oct 25 2022 5:41 AM

I have a table with the below structure and data.

sqlid logdate empcode timein1 timeout1 timein2 timeout2 timein3 timeout3 timein4 timeout4 timein5 timeout5
2 2022-08-24 5 10:15 14:03 14:18 19:41            
3 2022-08-24 6 11:02 13:57 14:41 15:48 15:55 16:19 16:33 19:45    
4 2022-08-24 7 9:35 11:13 11:31 16:47 17:13 18:08 18:55 19:23    
5 2022-08-24 9 10:03 12:45 12:56 14:05 14:41 18:11 18:22 20:04    
6 2022-08-24 10 9:51 10:31 11:48 13:52 14:28 16:41 17:21 18:25    
7 2022-08-24 12 9:58 13:52 14:28 16:24 16:40 17:20 18:13 18:36    
8 2022-08-24 16 9:24 13:17 13:56 19:01            
9 2022-08-24 18 9:41 10:57 12:42 13:53 13:59 17:13 17:40 17:58    
11 2022-08-24 29 9:19 17:37 22:53              
12 2022-08-24 31 9:57 13:17 14:02 14:18 15:55 16:10 16:31 18:41    

I want to convert the above data as below table structure, where the status column contains the direction of the log, IN or OUT (0 indicates IN and 1 indicates OUT). If timein has value then it shows as in time i.e. 0 and if timeout has value it will show as 1.

UserId LogDate LogTime LogDateTime Status
5 2022-08-24 10:15 2022-08-24 10:15:00.000 0
5 2022-08-24 14:03 2022-08-24 14:03:00.000 1
5 2022-08-24 14:18 2022-08-24 14:18:00.000 0
5 2022-08-24 19:41 2022-08-24 19:41:00.000 1
6 2022-08-24 11:02 2022-08-24 11:02:00.000 0
6 2022-08-24 13:57 2022-08-24 13:57:00.000 1
6 2022-08-24 14:41 2022-08-24 14:41:00.000 0
6 2022-08-24 15:48 2022-08-24 15:48:00.000 1
6 2022-08-24 15:55 2022-08-24 15:55:00.000 0
6 2022-08-24 16:19 2022-08-24 16:19:00.000 1
6 2022-08-24 16:33 2022-08-24 16:33:00.000 0
6 2022-08-24 19:45 2022-08-24 19:45:00.000 1

Please help me to write a SQL Server query to get the output.


Answers (1)