What is the best way to remove the first comma from the following string? The result should be ‘TEST,e,er,t’
DECLARE @source VARCHAR(100)
SET @source = ',TEST,e,er,t'
EDIT: what is the best way to remove all the leading commas, if there are more than one comma?
Thanks
Lijo
Personally I like T-SQL’s STUFF() function for this. One of the main reasons is that I don’t have to care about how long the string can be, swap out 8000 for 4000 if it is NVARCHAR, etc.
EDIT for new/changed requirements we might need to use SUBSTRING after all:
[Note that this will return all the commas if the string contains nothing but commas.]
You can of course do this with STUFF() still:
However this will return NULL for a string that is all commas, so you may want to wrap it in a COALESCE() if NULL is undesirable.