If I understand the question correctly, you want to have the values on separate rows.
To split the values there are quite a few ways to do it, some mentioned in previous solutions. If the date format contains leading zeroes for month and day then you can split the data based on the second space.
As an example, consider the following data
CREATE TABle #TheTable (
TheValue varchar(100)
);
INSERT INTO #TheTable VALUES ('2016-04-05 10.00 2016-04-05 8.00');
INSERT INTO #TheTable VALUES ('2016-04-06 11.00 2016-04-06 9.00');
INSERT INTO #TheTable VALUES ('2016-04-07 12.00 2016-04-07 10.00');
Now you could query the data with following query
WITH SeparatedColumns AS (
SELECT ROW_NUMBER() OVER (ORDER BY a.TheValue) AS RowNo,
SUBSTRING(a.TheValue, 1, CHARINDEX(' ', a.TheValue, 12)) AS StartCol,
SUBSTRING(a.TheValue, CHARINDEX(' ', a.TheValue, 12), 999) AS EndCol
FROM #TheTable a
)
SELECT b.RowNo,
CONVERT(datetime, REPLACE(TRIM(b.StartCol), '.', ':'), 120),
'In time'
FROM SeparatedColumns b
UNION ALL
SELECT b.RowNo,
CONVERT(datetime, REPLACE(TRIM(b.EndCol), '.', ':'), 120),
'Out time'
FROM SeparatedColumns b
ORDER BY 1, 2
In the CTE query the data is split into two columns. An extra column is added in order to see which values come from the same row. Otherwise you wouldn't know what dates should be handled together if the result set contains multiple rows.
In the outer part an UNION expression is used to fetch the columns on separate rows. In the end the data is ordered based on the original row number so that the rows from the same data are near to each other.
So the result would be
RowNo (No column name) (No column name)
----- ----------------------- -------------------
1 2016-04-05 08:00:00.000 Out time
1 2016-04-05 10:00:00.000 In time
2 2016-04-06 09:00:00.000 Out time
2 2016-04-06 11:00:00.000 In time
3 2016-04-07 10:00:00.000 Out time
3 2016-04-07 12:00:00.000 In time
However, if you have the possibility to modify the schema, I would suggest:
- Don't store two separate data items in the same column. Use separate columns
- Don't store dates in varchar columns, always use proper data type.