How to count the number of times a character appears in a SQL column?

前端 未结 2 1399
借酒劲吻你
借酒劲吻你 2020-12-14 00:39

For a user logging table I have in a SQL database, I track the some of the parameters off of a report request. The report allows multiple ID\'s to be passed to it and I sto

相关标签:
2条回答
  • 2020-12-14 00:58
    SELECT LEN(RequestedReportParams) - LEN(REPLACE(RequestedReportParams, ',', ''))
    FROM YourTable
    WHERE .....
    

    This is simply comparing the length of the column with the commas, with the length of the value with the commas removed, to give you the difference (i.e. the number of commas)

    0 讨论(0)
  • 2020-12-14 01:07

    It seems the quick and dirty way to answer the question you've been asked would be to do this:

    select 
        count(*) as cnt
    FROM 
        [table]
    WHERE 
        RequestedReportParams Like '%,%,%'
    
    0 讨论(0)
提交回复
热议问题