[MySQL] 특정 문자 개수 세기

in #mysql6 years ago (edited)

문자열 컬럼내 '@'의 갯수를 알아내려고 찾았을때 아래 구문을 쓰는 내용을 보았다.
정말이지 대한민국은 머리 좋은 사람이 많은것 같다.. ㅎㅎ

SELECT
SUM((CHAR_LENGTH(Paper_list)-CHAR_LENGTH(REPLACE(Paper_list,'@',''))+1))
FROM T_NewsInfo WHERE Notice_Number IN ('0999427', '0999426')

전체문자갯수에서 찾고자하는 문자열을 공백으로 처리한 문자열갯수를 뺌

CREATE FUNCTION getStrCount ( inputdata varchar(250), searchStr varchar(250) )
RETURNS int
BEGIN
    declare q int;
    set q = char_length(inputdata) - char_length(replace(inputdata,searchStr,'');
    RETURN(q);
END
select getStrCount (필드 또는 문자열,'찾고자 하는 문자열') from table;

MS-SQL

CREATE FUNCTION getStrCount (@inputdata nvarchar(max), @searchStr nvarchar(max) )
RETURNS int
AS
BEGIN
    DECLARE @q int
    SET @q = LEN(@inputdata) - LEN(REPLACE(@inputdata, @searchStr, ''))
    RETURN @q
END


1차 카테고리가 누락된 데이터 조회

Select goodsno
        , Left(category, 3) As 'depth1'
    , Group_Concat(category) As 'cate'
    , Max(Length(category)) / 3 As 'max_depth'
From gd_goods_link
Where Left(category, 3) = '005'
Group By goodsno, Left(category, 3)
Having Left(cate, 4) <> depth1+','
Select goodsno, depth1, cate, max_depth
    , Sum((Char_Length(cate)-Char_Length(Replace(cate,',',''))+1)) -- 콤마 개수
From (
    Select goodsno
        , Left(category, 3) As 'depth1'
        , Group_Concat(category) As 'cate'
        , Max(Length(category)) / 3 As 'max_depth'
    From gd_goods_link
    Where Left(category, 3) = '005'
    Group By goodsno, Left(category, 3)
    Having Left(cate, 4) <> depth1+','
) As A

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.030
BTC 70971.37
ETH 3805.38
USDT 1.00
SBD 3.42