One way to approach it is by joining the table to itself 3 times. There may be simpler ways, but this is the only one that comes to my mind right now :);WITH cte AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY NHS_No ORDER BY StartDate) AS RN
FROM YourTable
)
SELECT
a.NHS_No,
a.Cluster_no,
a.StartDate,
COALESCE(a.endDate,
CASE
WHEN DATEDIFF(dd,COALESCE(b.startDate,'19000101'),a.startDate) >
DATEDIFF(dd,a.startDate,COALESCE(c.startDate,'19000101')) THEN
b.startDate
ELSE c.startDate
END) AS EndDate
FROM
cte a
LEFT JOIN cte b ON b.RN+1 = a.RN
LEFT JOIN cte c ON c.RN-1 = a.RN
ORDER BY
a.NHS_No,
a.ClusterNo,
a.StartDate;