3

my string like that :

1  ,QCIM1J25836, QCIM1J27637
2  ,QCIM1J25836, QCIM1J27637, QCIM1J27638

I want to remove first comma only it means my output will be for

1  QCIM1J25836, QCIM1J27637
2  QCIM1J25836, QCIM1J27637, QCIM1J27638

may be in other will not be comma...so please tell me how can I update all data like that...

1
  • 4
    Which DBMS do you use? Commented Sep 8, 2015 at 5:36

4 Answers 4

3

In SQL-Server you can do It in multiple ways.

You use STUFF in following:

SELECT col1, 
       STUFF(col2,1,1,'') as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%'

or you can use RIGHT

SELECT col1, 
       RIGHT(col2,LEN(col2)-1) as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%';

or you can use SUBSTRING

SELECT col1,
       SUBSTRING(col2, 2, 255) as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%';

UPDATE

As per your comment you can update in the same ways too:

Using SUBSTRING

UPDATE tbl
SET col2 = SUBSTRING(col2, 2, 255)
WHERE col2 LIKE ',%';

Or using RIGHT

UPDATE tbl 
SET col2 = RIGHT(col2,LEN(col2)-1) 
WHERE col2 LIKE ',%';

Or using STUFF

UPDATE tbl
SET col2 = STUFF(col2,1,1,'')
WHERE col2 LIKE ',%';
Sign up to request clarification or add additional context in comments.

2 Comments

can I make update query for that ...I want to update data and remove first comma
@VipinSinghal and do not forget accept It as correct answer for future users If It helped for your. To do It you can mark V in top-left corner of answer.
3

The following query should work:
It will update all the records in the column.

UPDATE table
SET col2 = CASE WHEN LEFT(col2 ,1) =',' THEN RIGHT(col2,LEN(col2)-1)
                    ELSE col2 END

Comments

3
UPDATE table
SET col2 =  case when charindex(',',col2,0) =1 then right(col2, len(col2)-1) else col2 end

1 Comment

This answer is more logical
0
USE [LIB]
CREATE FUNCTION [dbo].[trimChar]
(
    @p_string  varchar(max),
    @p_char varchar(1)
)
RETURNS varchar(max)
AS
BEGIN
   declare @l_string varchar(max) = @p_string
   -- lets do the front
   while SUBSTRING(@l_string,1,1) = @p_char
   begin
      set @l_string = substring(@l_string,2,len(@l_string))
   end

   -- lets do the back
   set @l_string = reverse(@l_string)
   while SUBSTRING(@l_string,1,1) = @p_char
   begin
      set @l_string = substring(@l_string,2,len(@l_string))
   end
   set @l_string = reverse(@l_string)

   return @l_string

END
GO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.