Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:57:04+00:00 2026-06-13T08:57:04+00:00

I am trying to compare a database field which stores list items (comma separated)

  • 0

I am trying to compare a database field which stores list items (comma separated) with unfortunately a variable which is also a list item.

Example:

In this case, a user can belong to multiple groups, and content access is also allocated to multiple groups.

contentid | group  
 (1)   (c,d)   
 (2)   (a,c)  
 (3)   (b)

So, I need to select all content where user is in group (a,c). In this case, contentid 1,2 should be returned.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:57:05+00:00Added an answer on June 13, 2026 at 8:57 am

    Here’s a safe but slow solution for SQL 2008

    BEGIN
    -- setup
    DECLARE @tbl TABLE (
        [contentid] INT
        ,[group] VARCHAR(MAX)
    )
    INSERT INTO @tbl VALUES
         (1, 'c,d')
        ,(2, 'a,c')
        ,(3, 'd')
    -- send your request as simple xml
    DECLARE @param XML
    SET @param = '<g>a</g><g>c</g>'
    -- query
    SELECT DISTINCT contentid 
    FROM @tbl t
        INNER JOIN @param.nodes('/g') AS t2(g)
            ON ',' + t.[group] + ',' LIKE '%,' + t2.g.value('.', 'varchar(max)') + ',%'
    END
    

    You just pass your query in as an XML snippet instead of a comma separated list.

    If your group names are single characters or you can be sure the names are not character-subsets of each other (ie: GroupA, GroupAB), then the query can be optimized to.

    ON t.[group] LIKE '%' + t2.g.value('.', 'varchar(max)') + '%'
    

    If you’re using a RDBMS without XML parsing capability you’ll have to use string split your query into a temp table and work it that way.


    You really should not be using comma separated values inside your columns. It would be much better if the [group] column only contained one value and you had repeated entries with a UNIQUE constraint on the composite (contentid, group).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compare the value of the password stored in the database (which
I'm trying to compare a string field from a LINQ query on a database
I'm trying to compare two entries in a database, so when a user makes
I'm trying to compare 100,000 records on a local database (L) with 100,000 records
I am trying to compare a list of forbidden folders read from a file.
I am trying to compare two tools execution time, which I have installed in
I'm trying to compare some text to the text in a database. In the
i am trying to pull data from one table, compare columns to a variable,
I am trying to compare timestamps by finding out if particular timestamp field is
Trying to compare and print only members with Inactive status. The problem is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.