Given string:
Note: The following comma separted string is dynamic which comes with any combination.
DECLARE @Str varchar(max) = '[A-B],[B-C],[C-D],[D-E]'
Expected Result:
SUM([A-B]) AS [A-B],SUM([B-C]) AS [B-C],SUM([C-D]) AS [C-D],SUM([D-E]) AS [D-E]
My try:
SELECT 'SUM('+REPLACE(@Str,',','),SUM(')+')'
Output:
SUM([A-B]),SUM([B-C]),SUM([C-D]),SUM([D-E])