aman6496
aman6496

Reputation: 153

Remove last comma from dynamic sql

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],';

Upvotes: 5

Views: 7023

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270351

Rephrase your logic and remove the first one using stuff():

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

Then remove it as:

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

Upvotes: 1

Pரதீப்
Pரதீப்

Reputation: 93734

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

Upvotes: 4

Related Questions