wootscootinboogie
wootscootinboogie

Reputation: 8695

Count text fields T-SQL

Select myColumn, count(*) totalcount
from myTable
group by myColumn
having count(*) >1
order by count(*) desc;

The following works for counting integer fields, but not for text fields. How do I need to modify it so that it will work for text fields?

Thanks to all for their help!

Upvotes: 0

Views: 1563

Answers (2)

Joe Stefanelli
Joe Stefanelli

Reputation: 135838

For SQL Server 2005+, you could:

SELECT CAST(myColumn AS NVARCHAR(MAX)), COUNT(*) totalcount
    FROM myTable
    GROUP BY CAST(myColumn AS NVARCHAR(MAX))
    HAVING COUNT(*) >1
    ORDER BY COUNT(*) DESC;

Upvotes: 2

gbn
gbn

Reputation: 432361

I'm guessing you have trailing (or leading) spaces which are different values to trimmed values...

And you mean text values not datatype...

Upvotes: 2

Related Questions