Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a SQL table MENU with columns
SQL
Menu_id,Menu_Name,Rate
.
In the front end user can select n menus at their choice in checkboxes(CheckBoxList). I have written code to store all the menuId's for a user in a single column MenuId separated by commas (Tbl_Order: Order_Id,Customer_Id,MenuId).
Is it a good way to store these values like this in Sql server column?
Later I also need to find what all menus a customer has ordered. So how to Take individual ID's from comma separated MEnuID column?

Give me a solution soon.
Posted
Updated 19-Sep-14 0:11am
v2

My best advice is to learn how to normalize your database.
Your current design will give you lots of headaches in the future.

Database normalization[^]

3 Normal Forms Database Tutorial[^]

Database Normalization Basics[^]
 
Share this answer
 
Comments
CHill60 4-Oct-14 12:44pm    
Balanced that 1 with a +5. Very good advice.
George Jonsson 4-Oct-14 22:40pm    
Thanks. Don't think the OP liked the answer much, though.
try this

Select Menu_id+','+Menu_Name+','+Rate from Menu
 
Share this answer
 
Comments
George Jonsson 23-Sep-14 8:48am    
It is a bad idea to store comma separated data in a column.
KALYANI BIBIN 24-Sep-14 1:09am    
if i have a table named HOBBY having hobbyId,Hobby_name.
Along with userRegistration i want to store their Hobbies also in USERDetails Table. so what is the best way,to store multiple hobbies selected for single person?
Salman622 24-Sep-14 4:10am    
in hobby table Create one column Namely UserRegistrationID assign foreign key to it and in User Details Use ID as Primary key

when fetching data for particular user Use join
structure will be fine and Later there will be no permutations and Combinations
we want to concatenate one column row values into single row we need to write query like this

SQL
DECLARE @name NVARCHAR(MAX)
SELECT @name = COALESCE(@name + ',', '') + UserName FROM UserDetails
SELECT UserNames = @name


If you observe above query I used function COALESCE it returns the first non-null expression among its arguments.


SQL
Another way

If we want to concatenate rows of column we can write query like as shown below

DECLARE @name NVARCHAR(MAX)
SET @name=''
SELECT @name = @name +','+UserName  FROM UserDetails
SET @name = Substring(@name, 2, (len(@name)))
SELECT @name as UserNames



check this
 
Share this answer
 
Comments
George Jonsson 23-Sep-14 8:18am    
This is not a solution to the problem at hand.
The OP already had a comma separated column and wanted to insert and extract data.

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