(sql) how can I use count() method when data type is text?

后端 未结 3 1443
独厮守ぢ
独厮守ぢ 2021-01-16 22:35
select count(category) from list where category like \'action\'

above is a query i want to run. However, when I run that query, I am getting data

相关标签:
3条回答
  • 2021-01-16 23:00

    count() always return int like type.

    Read result data with int accessor: eg. getInt(1)

    0 讨论(0)
  • 2021-01-16 23:03

    You cant apply COUNT() function on text,ntext,image datatypes.

    Why you can't use :

    select count(*) from list where category like 'action'
    

    ? Do you have some nulls ?

    If you don't have to exclude null value the above query could already work well ...

    0 讨论(0)
  • 2021-01-16 23:14

    To answer the question in the title for future googlers you can use

    SELECT COUNT(CASE
                   WHEN some_text_column IS NOT NULL THEN 1
                 END)
    FROM   some_table
    

    In your case though (as pointed out in the comments by @hvd) the WHERE category LIKE 'action' predicate ensures that any NULL values will be excluded anyway so it is safe to replace it with COUNT(*)

    Moreover you almost certainly should not be using the text datatype for this. This is a deprecated datatype that was intended for holding LOB (Large Object) data over 8000 bytes.

    Strings like "action" definitely do not fit this description!

    0 讨论(0)
提交回复
热议问题