Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
its an hotel project., when user choose multi items for breakfast items from form i stored like 2,3,4,8 ., here 2 ,3 ,4, 8 are all ID's of item., now i want to separte these numbers to retrieve items.

in my database its store like

Table 1

menuitemsid|menuname
2,3,4,8 |Breakfast


Table 2

Itemid|item name
2 |Dosa
3 |Pizza
4 |Idly
5 |Muruku




i want output like

Breakfast
-------------
Dosa
Pizza
Idly
Muruku
Posted

That's a bit complex, because SQL server does not have a concept of arrays.
You will have to retrieve the string, and then break it into ID's and use that as part of an IN clause.
This procedure does something similar:
SQL
DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = '2,3,177,'
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ','
CREATE TABLE #tempTab (id int not null)
WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0
BEGIN
   SELECT  @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
   SELECT  @VALUE = LEFT(@INSTR , @SP - 1)
   SELECT  @INSTR = STUFF(@INSTR, 1, @SP, '')
   INSERT INTO #tempTab (id) VALUES (@VALUE)
END
SELECT * FROM myTable WHERE id IN (SELECT id FROM #tempTab)
DROP TABLE #tempTab


But you will have modify it to fit exactly what you want.
 
Share this answer
 
Comments
Umapathi K 17-Nov-12 8:14am    
in second line set @instr='2,3,177' you given static but when i try using select query (dynamic) i am getting error any idea about this?
OriginalGriff 17-Nov-12 8:17am    
Without the code yoiu are using? No idea at all...:laugh:
Create Function for separating Ids.

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]('1,2,3,4',',')


OutPut:
SQL
Data
1
2
3
4
 
Share this answer
 
Comments
Umapathi K 18-Nov-12 23:48pm    
select * from [dbo].[String_Tokenizer]('1,2,3,4',',')
here parameter values you have given static but in my case i am retrieving from database table something like., [select menu_items_id from <tablename>] its output like 2,4,5,6,7 (how can i use select query into the parameter).,
Shanalal Kasim 19-Nov-12 0:23am    
provide your exact query with where condition

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