如何计算字符在SQL列中出现的次数? [英] How to count the number of times a character appears in a SQL column?

查看:142
本文介绍了如何计算字符在SQL列中出现的次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于用户记录表,我在SQL数据库中,我跟踪报告请求的一些参数。该报告允许将多个ID传递给它,并将所有这些ID存储在数据库列中的单个列中。如果这是一个规范化的数据集,肯定会有一个额外的表设置,但这是继承...



我现在被要求快速计算报告运行的次数,超过2个ID传递给它。我可以很容易地获得多于1个报告请求的记录数,因为它们都包括一个逗号。



接下来我需要计算逗号在列中出现的次数。如何在SQL中执行此操作?

   - 计算请求多个报告的次数记录
select
count(*)as cnt
from
[table]
其中
RequestedReportParams类似'%,%'


解决方案

  SELECT LEN(RequestedReportParams) -  LEN (REPLACE(RequestedReportParams,',',''))
FROM YourTable
WHERE .....

这只是比较列的长度与逗号,删除逗号的值的长度,以给出差异(即逗号的数量)


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 store all of those in a single column in the database column. If this were to be a normalized set of data, there would definitely be an additional table setup for this, but this is what was inherited...

I've now been asked to give a quick count of the number of times a report was run with more than 2 ID's passed to it. I can easily get the number of records that have more than 1 report requested because they all include a comma.

What I need to do next is count the number of times a comma appears in a column. How do you do this in SQL?

--count the number of times more than 1 report was requested in the record
select 
    count(*) as cnt
from
    [table]
where
    RequestedReportParams Like '%,%'

解决方案

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)

这篇关于如何计算字符在SQL列中出现的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆