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

  • SEARCH
  • Home
  • 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 8453423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:46:14+00:00 2026-06-10T11:46:14+00:00

Environment: Sql Server 2008 have rows of a column that contains comma separated values.

  • 0

Environment: Sql Server 2008

have rows of a column that contains comma separated values.

what to get the row even if a single product exists in that csv.

this is how i can do it but was just wondering if another way to write it?

SELECT * FROM REWARDS
WHERE ProductCsv like '%banana%' 
   or ProductCsv like '%strawberry%' 
   or ProductCsv like '%orange%'
  • 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-10T11:46:16+00:00Added an answer on June 10, 2026 at 11:46 am

    Your current query doesn’t seem to accurately capture the results you want. What if we have a row like this:

    bananaberry cream pie,strawberry shortcake,orange juice
    

    Should this match your query or not? I think this would be more accurate:

    WHERE ',' + ProductCsv + ',' LIKE '%,banana,%'
       OR ',' + ProductCsv + ',' LIKE '%,strawberry,%'
       OR ',' + ProductCsv + ',' LIKE '%,orange,%'
    

    If you’re just trying to find one item, this is probably much more efficient:

    WHERE ',' + ProductCsv + ',' LIKE '%,banana,%'
    

    You probably want to use a split function. For example:

    CREATE FUNCTION dbo.SplitStrings_XML
    (
       @List   NVARCHAR(MAX),
       @Delim  NVARCHAR(255)
    )
    RETURNS TABLE
    WITH SCHEMABINDING
    AS
       RETURN 
       (  
          SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
          FROM 
          ( 
            SELECT x = CONVERT(XML, '<i>' + REPLACE(@List, @Delim, '</i><i>') 
            + '</i>').query('.')
          ) AS a CROSS APPLY x.nodes('i') AS y(i)
       );
    GO
    

    So now you can say:

    SELECT * FROM dbo.REWARDS AS r
    WHERE EXISTS 
    (
      SELECT 1 FROM dbo.REWARDS AS r2
      CROSS APPLY dbo.SplitStrings_XML(r2.ProductCsv, ',') AS x
      WHERE x.Item IN ('orange','strawberry','banana')
      AND r2.[key] = r.[key]
    );
    

    Or more simply:

    SELECT DISTINCT r.ProductCsv --, other columns
      FROM dbo.REWARDS AS r
      CROSS APPLY dbo.SplitStrings_XML(r.ProductCsv, ',') AS x
      WHERE x.Item IN ('orange','strawberry','banana');
    

    The XML approach is a little brittle depending on the kinds of strings can be stored in the table, but there are many alternatives (including passing in your set via a TVP instead of as a list or separate values). For some much deeper background on split functions see these blog posts:

    http://www.sqlperformance.com/2012/07/t-sql-queries/split-strings

    http://www.sqlperformance.com/2012/08/t-sql-queries/splitting-strings-follow-up

    http://www.sqlperformance.com/2012/08/t-sql-queries/splitting-strings-now-with-less-t-sql

    That all said, I don’t know if this is any better than what you have now.

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

Sidebar

Related Questions

I have a SQL SERVER 2008 R2 on development environment that connect to production
I have a SQL Server 2008 database (call it productionDB) that contains data and
I have a SQL Server 2008 in production environment (Windows 2003 -64 bit) and
We have a client environment that has MSDE. We're looking to install SQL Server
My dev environment has SQL Server 2008 installed. I have an asp.net 1.1 site
I have a table with 300 million rows in Microsoft SQL Server 2008 R2.
Environment: SQL Server 2005/2008, pubs database I have inserted into a table variable a
I have two tables in a SQL Server 2008 environment with the following structure
I have a SQL Server 2008 db. It contains some great test data...a proposal
I have a synchronous mirroring environment with automatic fail-over using sql server 2008 standard

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.