2

I want to insert my split string into my table as you can see :

create table #Organization
(
    organizationId bigint, 
     provienceId bigint, 
     CityId bigint, 
      TownId bigint 

)
Insert Into #Organization  ( organizationId) 
 select  p.value from  string_split('1,2,3', ',') p
Insert Into #Organization  ( provienceId) 
 select  p.value from  string_split('1,2,3', ',') p
Insert Into #Organization  ( CityId) 
 select  p.value from  string_split('1,2,3', ',') p
Insert Into #Organization  ( TownId) 
 select  p.value from  string_split('1,2,3', ',') p

My expected result is something like this :

1   1     1   1
2   2     2   2
3   3     3   3

But it returns this :

enter image description here

6
  • 1
    You need an update statement for second and third statement Commented Jan 12, 2019 at 14:05
  • @PrashantPimpale how can i update the records? Commented Jan 12, 2019 at 14:06
  • 2
    Is there only three values every time or this is not real data? Commented Jan 12, 2019 at 14:07
  • @PrashantPimpale it is not real data it can be changed Commented Jan 12, 2019 at 14:14
  • yes exactly, So can you provide sample data that you are actually dealing with? Commented Jan 12, 2019 at 14:16

3 Answers 3

3

Perhaps I'm reading into your question, but I suspect you want to split your string into columns

Example

Declare @YourTable table (SomeColName varchar(max)) 
Insert Into @YourTable values 
 ('1,2,3')
,('A,B,C')
,('Dog,Cat,Pony')

Select B.*
 From  @YourTable A
 Cross Apply (
                Select Pos1 = ltrim(rtrim(xDim.value('/x[1]','varchar(max)')))
                      ,Pos2 = ltrim(rtrim(xDim.value('/x[2]','varchar(max)')))
                      ,Pos3 = ltrim(rtrim(xDim.value('/x[3]','varchar(max)')))
                From  (Select Cast('<x>' + replace((Select replace(SomeColName ,',','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as A 
             ) B

Returns

Pos1    Pos2    Pos3
1       2       3
A       B       C
Dog     Cat     Pony
Sign up to request clarification or add additional context in comments.

6 Comments

if i want to change the 1 2 3 ,4 5 6 , 7 8 9 to 1 4 7 ,2 5 8 , 3 6 9 ,what should i do ?
Are you looking for matrix manipulations? Perhaps edit your question and add a more distinct data sample and desired results.
@EhsanAkbar Happy to help ... I guess :)
Hi again ,in fact in my question i expect these result ,1 1 1 1,2 2 2 2 ,3 3 3 3 ,could you please help me how can i get this result?
i changed my question
|
2

Insert is inserting three rows, one for each value. If you want to combine them into columns, you need to pivot them or (as I prefer) use aggregation.

You also have a table with four columns but only seem to be inserting three, so I suspect you want:

create table #Organization (
    organizationId int identity(1, 1) primary key, 
    provinceId int, 
    CityId int, 
    TownId int 
);

(I don't see a need for bigint for this example.)

Obviously, the simplest solution is not to use strings:

Insert Into #Organization (provinceId, CityId, TownId) 
    values (1, 2, 3);

But if you are using strings, you can try:

Insert Into #Organization (provinceId, CityId, TownId) 
    select provinceId, CityId, TownId
    from (values('1,2,3')) v(str) cross apply
         (select max(case when seqnum = 1 then p.value end) as provinceid,
                 max(case when seqnum = 2 then p.value end) as cityid,
                 max(case when seqnum = 3 then p.value end) as townid                 
          from (select p.*,
                       row_number() over (order by charindex(',' + p.value + ',', ',' + v.str + ',')) as seqnum
                from string_split(v.str, ',') p
               ) p
         ) s;

Note that there is an issue with using string_split() for this purpose, because it does not "remember" the position of the substring in the original string. This attempts to get around this problem by using charindex(). In your case, this will work, because the values are all numbers.

Here is a db<>fiddle.

3 Comments

Expecting an answer from you!
Thank you ,but i want to say the 1,2,3 is an example it can be changed to other values
@EhsanAkbar . . . And nothing in this answer precludes that. Did you look at the db<>fiddle? It has examples of other values.
0

I would use pivot to transform the columns into rows. Like:

INSERT INTO #organization 
            (provinceid, 
             cityid, 
             townid) 
SELECT * 
FROM   (SELECT p.value, 
               RowN = Row_number() 
                        OVER ( 
                          ORDER BY (SELECT NULL)) 
        FROM   String_split('1,2,3', ',') p) a 
       PIVOT (Max(a.value) 
             FOR rown IN ([1], 
                          [2], 
                          [3])) b 

1 Comment

Hi again ,in fact in my question i expect these result ,1 1 1 1,2 2 2 2 ,3 3 3 3 ,could you please help me how can i get this result?

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.