How could i remove the last comma from a part of dynamic query
set @Query += '[A].[ID].&[' + Convert(varchar,SUBSTRING(@string, @start, @end - @start) ) +']&[CAN],[A].[ID].&[' +Convert(varchar,SUBSTRING(@string, @start, @end - @start) ) + ']&[usa],';
To remove all characters after the last comma in the string, you can use SUBSTRING_INDEX().
One common technique uses Left
and Len
function
set @Query = Left(@Query,len(@Query)-1)
Update : Run the above statement after competition of while loop/Cursor or after framing the entire query
Rephrase your logic and remove the first one using stuff()
:
set @Query = ',[A].[ID].&[' + . . . ';
Then remove it as:
set @Query = stuff(@Query, 1, 1, '');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With