-1

I have two rows in column like this:

  1. , Josiah Tongari Street, Labone, Accra Metropolitan, Greater Accra, Ghana
  2. , , Accra, Greater Accra, Ghana

So I want to remove first comma from first one and remove first two from 2nd one. I want output like this egxample:

  1. Josiah Tongari Street, Labone, Accra Metropolitan, Greater Accra, Ghana 2.Accra, Greater Accra, Ghana

Thanks

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121

1 Answers1

1

First, read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
After reading that, you should normalize your database.

If you can't normalize your database, then you can use STUFF and PATINDEX to remove the first commas.

Create and populate sample table (Please save us this step in your future questions)

DECLARE @T AS TABLE
(
    col varchar(100) 
)

INSERT INTO @T VALUES 
('a, b,c'), 
(', d, e,f'), 
(', , g, h,i, j')

The query:

SELECT  STUFF(col, 1, PATINDEX('%[^, ]%', col)-1, '') As col
FROM @T

Results:

col
a, b,c
d, e,f
g, h,i, j
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • [Glad to help :-)](http://meta.stackoverflow.com/questions/291325/how-to-show-appreciation-to-a-user-on-stackoverflow/291327#291327) – Zohar Peled Jul 26 '17 at 08:40