Remove last comma from dynamic sql

How can I remove the last comma from part of a dynamic query

set @Query += '[A].[ID].&[' + Convert(varchar,SUBSTRING(@string, @start, @end - @start) ) +']&[CAN],[A].[ID].&[' +Convert(varchar,SUBSTRING(@string, @start, @end - @start) ) + ']&[usa],';
+4
source share
2 answers

One general method uses functions LeftandLen

set @Query = Left(@Query,len(@Query)-1)

Update: Run the above statement after the while loop / Cursor contest or after cropping the entire request

+1
source

Rephrase your logic and delete the first with stuff():

set @Query = ',[A].[ID].&[' + . . . ';

Then delete it as:

set @Query = stuff(@Query, 1, 1, '');
0
source

Source: https://habr.com/ru/post/1667868/


All Articles