Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I take the first and last item of string in sql query result

Tags:

sql

sql-server

I have a log table of staff entrance and exit dates and time like below;

  • 11/12/2007 12:23,11/12/2007 21:22,...,11/12/2007 22:24
  • 12/12/2007 09:11,12/12/2007 11:34,...,12/12/2007 17:15
  • ...continues

Number of items are different and all entries are in daily based. Minimum entry will be 2 because of entrance and exit logs.

I want to take only the start and end date from the logs. Please help me about the T-SQL query...

like image 811
Mr MCPD Avatar asked Aug 19 '17 23:08

Mr MCPD


1 Answers

Another Simple Solution of your problem use Left and Right Function

DECLARE @str NVARCHAR(MAX)='11/12/2007 12:23,11/12/2007 21:22,11/12/2007 22:24'

SELECT LEFT(@str, CHARINDEX(',',@str) -1), Right(@str, CHARINDEX(',', Reverse(@str)) -1)

like image 127
santosh Avatar answered Nov 14 '22 23:11

santosh