SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


un split in sql


un split in sql

Author
Message
elham_azizi_62
elham_azizi_62
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2959 Visits: 820
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.
bitbucket-25253
bitbucket-25253
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34605 Visits: 25280
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,




If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
GilaMonster
GilaMonster
SSC Guru
SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)

Group: General Forum Members
Points: 546052 Visits: 47729
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)

Group: General Forum Members
Points: 504457 Visits: 44233
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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
justmohit
justmohit
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 78
You can use XML PATH for the inverse function in which input is table of string and output is comma seperated value.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)

Group: General Forum Members
Points: 504457 Visits: 44233
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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)SSC Guru (504K reputation)

Group: General Forum Members
Points: 504457 Visits: 44233
@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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
elham_azizi_62
elham_azizi_62
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2959 Visits: 820
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
yuvipoy
yuvipoy
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6113 Visits: 1497
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!
elham_azizi_62
elham_azizi_62
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2959 Visits: 820
thanks alot from you.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search