Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to know if it is at all possible to check if a value exit in a VARCHAR column when using the SELECT statement.

e.g:
VARCHAR Column (Items) = "Item1, Item2, Item3, Item4"
(Items are separated by a comma (,).

I would like to know if "Item3" exist during a SELECT statement.

What I have tried:

I am still researching this topic and not finding much information.
Posted
Updated 8-May-16 23:33pm

Yes...you can...but...
You can use LIKE:
SQL
SELECT * FROM MyTable WHERE myColumn LIKE '%Item3%'

But...that's not a good idea.
The problem is that your data is badly stored - you shouldn't store "lists" in a single column. Instead, you should have a separate table which stores the list items separately, with a foreign key back to the row in your other table. That way, each list item is easily found, and you can retrieve them all with a simple JOIN.

Storing comma separated lists in VARCHAR columns is a PITA to work with and always gives more trouble than it solves. For example, how do you remove "Item2" from your example, and add "Item5" instead? If you store it properly, it's trivial!
 
Share this answer
 
Comments
Tino Fourie 9-May-16 5:55am    
OG, as always you read my intentions, my intention was indeed to store "Items" as a list from where I could manage the content of the column in my application. My experience with SQL is limited and I know that your advice is ALWAYS sound as I have found with a previous question and you pointed towards Stored Procedures.

In the application, the User can only add to the list as it is part of a Subscription section - unsubscribing has no benefit and therefore there is no option to unsubscribe.

That all said, I will rather look into your advice and design a separate table for the Items and learn to use the JOIN function with the SELECT statement.

Thanks again for the "push" into the correct direction. ;)
OriginalGriff 9-May-16 6:02am    
You're welcome!
Sure, but with caveats:

You could do it like this:
SQL
DECLARE @text NVARCHAR(MAX);
SET @text = 'Item3'

SELECT t.* 
FROM   table t
WHERE  t.Items like '%'+@text+'%'


That is 'ok' but far from great. It will identify Item3 AND Item35.

You could try using the commas in a multipart select:

SQL
DECLARE @text NVARCHAR(MAX);
SET @text = 'Item3'

SELECT t.* 
FROM   table t
WHERE  t.Items like @text+',%'
OR     t.Items like ', %'+@text+',%'
OR     t.Items like ', %'+@text
OR     t.Items = @text


This uses the comma to check if the text is an item, at the start or end of a collection or if it is the only item.

However, and here comes the caveat, text bases searches can be very slow, especially if they are large or you have many items to find. It may be better to convert the csv columns into tables with a tabular function like the one linked here:
Convert a CSV delimited string to table column in SQL SERVER[^]

This could also be slow too. A combination of the first "like" query and this one may be more efficient.


My long term advice is to not store csv in a table column. It should be simple enough to store csv values in a child table. This is far more efficient especially if the Items are not unique to any one record. However, I am aware of many reasons why you cannot redesign the db at this stage.
EDIT: You should read OriginalGriff's points on this issue

I hope that helps ^_^
Andy
 
Share this answer
 
v2
Comments
Tino Fourie 9-May-16 6:01am    
Andy, thank you very much for your contribution. I have looked at OG's solution and would rather venture down that avenue for now. It seems easier to read and with the JOIN option it makes readability much easier as well.

I am fortunate to redesign the DB as it is a brand new DB and still growing as new information is added or Application requirements are added / removed / changed.

Thanks again!!
Andy Lanng 9-May-16 6:32am    
No problem. Thanks for the note. OG's solution is much better if you can redesign ^_^

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