Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a keywords table where in keywords column i have
Business Solutions + Business Solutions in Bangalore + Business Solutions in Mysore

I want to split these keywords using functions.. How to achieve this. Pls provide the solution.
Posted
Updated 9-Aug-16 16:59pm
Comments
gvprabu 27-Mar-13 3:20am    
Hi,

Check the Post... (My Answer)
http://www.codeproject.com/Answers/567667/Splitplusfunctionpluserrorplus-plusTheplusstatemen#answer1

 
Share this answer
 
Hi,

Create below function

SQL
CREATE FUNCTION [dbo].[String_Tokenizer]
(
	@RowData nvarchar(max),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
	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



The above function will return a temporary table.

Sample code:

SQL
select * from [dbo].[String_Tokenizer]('Business Solutions + Business Solutions in Bangalore + Business Solutions in Mysore','+')


Output:
SQL
Data

Business Solutions
Business Solutions in Bangalore
Business Solutions in Mysore
 
Share this answer
 
v2
Comments
Member 7767311 27-Mar-13 7:32am    
k i accept the solution .. but how to call this function in stored procedure

Regards
Sarthak
Shanalal Kasim 27-Mar-13 7:46am    
please mark this as answer.
In sp you can use like
select * from [dbo].[String_Tokenizer] (parameter)
Or
declare table variable like Declare @Tmp table ( Data nvarchar(100) )
then
insert into @Tmp select * from [dbo].[String_Tokenizer](parameter)

Other wise please explain your scenario
If you have the knowledge, and the possibility to do that, I suggest, you make a .net library with CLR User Defined Functions you might need (see: http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.80).aspx[^]). In .net, you have so much more possibilities to resolve such simple tasks.
Here is an example, ho to make table-valued clr udf: http://msdn.microsoft.com/en-us/library/ms131103.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900