October 7, 2012 at 4:03 am
hello all.
i use this function for split:
ALTER FUNCTION dbo.GLB_Split(@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (id int identity(1,1),items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
but i want to write function split inverse and no with cursor,please help me.
my target is:
input of function :tables of string
output of function:string that seprate with comma like this:a,b,c,d
please help me how do i do and i donot want cursor.
October 7, 2012 at 6:04 am
Is this what you require ?
DECLARE @String VARCHAR(8000),@Delimiter CHAR(1)
SET @Delimiter = '-'
SET @String = 'A-ab-xx-y-vv-w w-'
select @String, REPLACE(@String,@Delimiter,',')
Result:
(Orinial) (Replaced)
A-ab-xx-y-vv-w w- A,ab,xx,y,vv,w w,
October 7, 2012 at 6:08 am
Take a look at the last option here: http://databases.aspfaq.com/general/how-do-i-concatenate-strings-from-a-column-into-a-single-row.html
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 7, 2012 at 8:37 am
elham_azizi_62 (10/7/2012)
but i want to write function split inverse and no with cursor,please help me.
I'm a bit confused here. You use a split function that is a slow scalar UDF that uses a While Loop (the guts of a cursor). My recommendation would be to fix that bad boy first.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 7, 2012 at 12:34 pm
You can use XML PATH for the inverse function in which input is table of string and output is comma seperated value.
October 7, 2012 at 6:26 pm
justmohit (10/7/2012)
You can use XML PATH for the inverse function in which input is table of string and output is comma seperated value.
That's real nice. Do you have a code example or a link or something?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 7, 2012 at 6:30 pm
@elham,
The following link has some excellent examples of how to do as you ask and it explains the "why".
http://www.sqlservercentral.com/articles/comma+separated+list/71700/
--Jeff Moden
Change is inevitable... Change for the better is not.
October 7, 2012 at 10:41 pm
I want to have this:
select column1 from table1
column1
a
b
c
and result:a,b,c
and i donot want to use cursor or virtual table.please help me.thanks
October 7, 2012 at 11:41 pm
declare @Temp Table(Col int)
Insert into @Temp
Select 1
UNION Select 2
UNION Select 3
UNION Select 4
UNION Select 5
Select * from @Temp
Declare @coalesce varchar(200)
Set @coalesce=''
Select @coalesce=@COALESCE+Convert(varchar,Col)+',' from @Temp
Select @coalesce=left(@COALESCE,LEN(@COALESCE)-1)
Select @coalesce
Thanks!
October 8, 2012 at 1:04 am
thanks alot from you.
October 8, 2012 at 1:45 am
elham_azizi_62 (10/7/2012)
I want to have this:select column1 from table1
column1
a
b
c
and result:a,b,c
and i donot want to use cursor or virtual table.please help me.thanks
GilaMonster (10/7/2012)
Take a look at the last option here: http://databases.aspfaq.com/general/how-do-i-concatenate-strings-from-a-column-into-a-single-row.html
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply