sqlsql-servert-sqlsplitdataexplorer

Counting the number of tags in a single post - data explorer


I am trying to write a query that will count the number of tags under a single post. I somehow cannot wrap my head around splitting up the tags in a single row and counting those tags.

Other posts suggest creating a function to split the tags up, but you can't create functions on data exchange.

Here is the query that I have so far, which will just give me the posts that are in both Posts and Tags.

SELECT p.Id, p.Title, p.Tags, t.TagName
FROM Posts as p 
INNER JOIN Tags as t ON p.Id = t.Id

Solution

  • You could count number of tags using Tags column:

    SELECT Id, Title, Tags, LEN(Tags) - LEN(REPLACE(Tags, '<','')) AS tag_number
    FROM Posts as p 
    WHERE Id = 2647
    

    SEDE Demo