Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 A Filter condition in Split function....

Author  Topic 

kka_anand
Starting Member

24 Posts

Posted - 2010-08-08 : 15:40:04
Hi All,

I have taken the below code from the following link http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

CREATE FUNCTION dbo.Split
(
@RowData nvarchar(300),
@SplitOn nvarchar(1)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1

While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End


Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))

Return
END



when I execute the function,
select * from Split('1,22,333,444,5555,666', ',')

it displays the result as

Id Data
1 1
2 22
3 333
4 444
5 5555
6 666


It's possible to filter the result with a condition?
display the result for the Id=3.
i.e

Id Data
3 333


Regards
Anand

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-08-08 : 17:14:58
select * from Split('1,22,333,444,5555,666', ',') as w
where id = 3




N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

kka_anand
Starting Member

24 Posts

Posted - 2010-08-09 : 04:01:13
Thanks...Peso.

I want the filter condition inside the fuction.

Go to Top of Page
   

- Advertisement -