0

I want split text from NAME column and insert comma separated data to PARCA column for each row. ex:

name            parca
----            -------------
john            j,jo,joh,john

Code:

DECLARE @i int = 0
WHILE @i < 8
BEGIN 
SET @i = @i + 1
update export1 set PARCA = cast ( PARCA as nvarchar(max))  + cast (substring(NAME,1,@i) as nvarchar(max) ) +',' 
FROM export1 
end

There are two things I can't do;

  1. I could not equalize the @i value to name row count
  2. I could not checked NAME column whether the value in PARCA column
1
  • What do you mean by cell? Database column? Commented Feb 4, 2014 at 7:54

4 Answers 4

1

Create this function:

create function f_parca
(
 @name varchar(100)
) returns varchar(max)
as
begin
declare @rv varchar(max) = ''

if @name is not null
select top (len(@name)) @rv += ','+ left(@name, number + 1) 
from master..spt_values v
where type = 'p'

return stuff(@rv, 1,1,'')
end

Testing the function

select dbo.f_parca('TClausen')

Result:

T,TC,TCl,TCla,TClau,TClaus,TClause,TClausen

Update your table like this:

UPDATE export1
SET PARCA = dbo.f_parca(name)
Sign up to request clarification or add additional context in comments.

6 Comments

thank you for your interest. how can use this function as foreach parca column update? is there any idea?
@user3269841 yes, read last 2 code lines, that will update all rows
yes, i am seeing. but there is error Msg 1014, Level 15, State 1, Line 2 TOP clause contains an invalid value.
@user3269841 I think you have a null value in your table. I have taken that into account now
yes. i have null value in my table. can i skipping null values?
|
1
DECLARE @Count INT,@I INT
SET @I = 1
SET @Count = LEN('SURESH')
DECLARE @N VARCHAR(2000)
SET @N = ''
WHILE @Count > 0
BEGIN
    SET  @N = @N + ','+SUBSTRING('SURESH',1,@I)
    SET  @I = @I+1
    SET @Count  = @Count -1
END

SELECT SUBSTRING(@N,2,2000)

The above code is only a sample.'SURESH' is your name field.from which you can pass your own name values.Instead of final select u can put ur update.

5 Comments

+1 but, eliminating the first comma may be better from @N instead of using substring.
I suppose each name will have a different length... this will solve only part of the problem. The table has to be read through a cursor.
@semihyagcioglu How you eliminate forst comma without using substring?
Sorry for the confusion, substring is OK, but CHARINDEX may be a better fit though.
Thanks for your replies, so, how can i for each column update? not as varchar, as returned sql row group
0

Try this, this query will break the word into characters rows as expected then you can merge into a single row

DECLARE @Name AS Varchar(100)='Naveen'  

;with cte as
(
select 1 AS Counter,CAST(SUBSTRING(@Name, 1, 1) AS Varchar(100)) Name
union all 
select Counter+1,CAST((Name + ',' + SUBSTRING(@Name, Counter+1, 1))AS Varchar(100)) Name
from cte
where Len(Name) < Len(@Name) + (Len(@Name) -1)
)
select
    Name
   from cte
option(MAXRECURSION 0) 

Comments

0
-- This query will give you exactly what you are looking for, use Emp Table with Ename as     column    

;with cte as
(
select  1 AS Counter,EName,CAST(SUBSTRING(E.EName, 1, 1) AS Varchar(100)) Name
        From EMP E 
union all 
select Counter+1,E.EName,CAST((Name + SUBSTRING(E.EName, Counter+1, 1))AS Varchar(100)) Name
From EMP E
     INNER JOIN CTE C ON C.Ename=E.EName
where Len(Name) < Len(E.EName) 
)
select EName AS Name,
   STUFF((    SELECT ',' + Name AS [text()]                        
                        FROM CTE A
                        WHERE
                        A.EName = cte.EName
                        FOR XML PATH('')
                        ), 1, 1, '' )                       
            AS Parca    
   from cte 
   Group By EName
   Order By EName
   option(MAXRECURSION 0) 

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.