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 9203131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:24:14+00:00 2026-06-17T23:24:14+00:00

I have a table Article which has one column keywords which stores tags or

  • 0

I have a table Article which has one column keywords which stores tags or keywords

Sample Data

Table Article
------------------------------------------
ID    keywords
------------------------------------------
1     one, two, three
2     four, five, six
3     seven, eight, three
4     one, two, three
5     twenty, hundred, one hundred one, one hundred two, seventy
6     seventy, three, two hundred 

If I use a CTE query as below then it will concatenate all the keywords column in one row but on other side also gets duplicate row, which is a problem as I we will have 1000’s of article with hundreds of similar keywords.

SELECT TOP 1 
    stuff(
    (
    select cast(',' as varchar(max)) + lower(Keywords)
    from art_Article a
    for xml path('')
    ), 1, 1, '') AS All_Keywords
FROM
    art_Articles G
ORDER BY
    G.ArticleKeywords ASC;

Above query yields follow result as single row

---------------------------------------------------
All_Keywords
----------------------------------------------------
one, two, three,four, five, six,seven, eight, three,one, two, three,twenty, hundred, one hundred one, one hundred two, seventy,seventy, three, two hundred 

It is obvious from the result that it also show duplicate keywords no. of time as it is stored in row. Is their a way I can get duplicate keywords only once?

I would appreciate if some can help me to sort this to get the result as single column with DISTINCT keywords only.

  • 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-17T23:24:15+00:00Added an answer on June 17, 2026 at 11:24 pm

    I think you are going to have to split the data up into rows first, before concatenating it together. This will allow you to get the distinct values when using FOR XML PATH to get the final list.

    Here is a CTE version of this process:

    ;with cte (id, word, words) as
    (
      select id,
        cast(left(keywords, charindex(',',keywords+',')-1) as varchar(50)) word,
             stuff(keywords, 1, charindex(',',keywords+','), '') words
      from article
      union all
      select id,
        cast(left(words, charindex(',',words+',')-1) as varchar(50)) word,
        stuff(words, 1, charindex(',',words+','), '') words
      from cte
      where words > ''
    ) 
    select top 1 
        stuff((select distinct ', '+ rtrim(ltrim(word))
               from cte
               for xml path('')), 1, 1, '') AS All_Keywords
    from cte
    

    See SQL Fiddle with Demo. Which gives the result:

    |                                                                                                             ALL_KEYWORDS |
    ----------------------------------------------------------------------------------------------------------------------------
    |  eight, five, four, hundred, one, one hundred one, one hundred two, seven, seventy, six, three, twenty, two, two hundred |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataSet which has one table loaded with data. When i write
So I have two tables, article and comments (which has one-to-many relationship (1 article
I have a table articles that has a column company which has list of
I have two tables, one which stores articles and the number of votes it
I have one table, e.g. pricerules, that have stored special prices by article for
In my PHP application, I have a mysql table of articles which has the
I have table called articles_tags which have two columns: article_id tag_id (has_and_belongs_to_many association) (I
I have two tables. One is article and the other structure. And the articles
I have a Day Strucuture Table , which has following Columns I want to
I am using MySQL and have an articles table which has 2 columns: currency

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.