Hezron Naresh
Hezron Naresh

Reputation: 79

Get specific word count in particular column by SQL Server

I have to get the count of specific word from the column in the table.

Example : assume this value is in the column:

uid-234,uid-342,uid-345

I need to retrieve the count as 3 by using T-SQL in SQL Server.

Upvotes: 0

Views: 566

Answers (3)

Eralper
Eralper

Reputation: 6612

You can try following code

select 
    *, (select count(*) from dbo.Split(concatenatedColumn,',')) cnt
from myTable

But you need to create the user defined function SPLIT string on your database first

Upvotes: 0

StackUser
StackUser

Reputation: 5398

Try this,

DECLARE @Column VARCHAR(100) = 'uid-234,uid-342,uid-345'

SELECT len(@Column) - len(replace(@Column, ',', '')) + 1 AS TotalCount

Upvotes: 1

Munavvar
Munavvar

Reputation: 821

Try this, It should work

SELECT SUM(len(YourColumn) - len(replace(YourColumn, ',', '')) +1) 
    AS TotalCount
FROM YourTable

Upvotes: 1

Related Questions