1

Please suggest me an SQL Query for the Below I have Ids of Items Stored in one column like this 1,2,3,4 as Delimited or CSV value.
Now i want count no of items in one particular row in one particular cell.

i.e If i have 25,45,26,45,46 in a cell in table.
I want the output like 5 which is nothing but the number of items in a row in a cell say (0,0). Thank you so much for your Replies

user1093513
  • 127
  • 2
  • 4
  • 12
  • A properly normalized database would make this unecessary – John Conde May 24 '12 at 13:11
  • 1
    Duplicate: http://stackoverflow.com/questions/7020001/how-to-count-items-in-comma-seperated-list-mysql – Ike Walker May 24 '12 at 14:01
  • Possible duplicate of [How to count items in comma separated list MySQL](https://stackoverflow.com/questions/7020001/how-to-count-items-in-comma-separated-list-mysql) – LWC May 21 '18 at 20:00

1 Answers1

4

Assuming you don't have values like '123,123,' (note the comma at the end) this should work:

SELECT
LENGTH(yourColumn) - LENGTH(REPLACE(yourColumn, ',', '')) + 1 AS numberOfItemsInRow
FROM yourTable;

Find more information here.

But it would really be better to normalize your database!

fancyPants
  • 50,732
  • 33
  • 89
  • 96