How can I count the number of words in a string in Oracle?

前端 未结 4 1470
独厮守ぢ
独厮守ぢ 2020-12-11 16:49

I\'m trying to count how many words there are in a string in SQL.

Select  (\"Hello To Oracle\") from dual;

I want to show the number of wor

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 17:40

    DECLARE @List       NVARCHAR(MAX) = '   ab a 
    x'; /*Your column/Param*/
    DECLARE @Delimiter  NVARCHAR(255) = ' ';/*space*/
    DECLARE @WordsTable TABLE (Data VARCHAR(1000));
    
    /*convert by XML the string to table*/
    INSERT INTO @WordsTable(Data)
    SELECT Data = y.i.value('(./text())[1]', 'VARCHAR(1000)')
    FROM 
    ( 
    SELECT x = CONVERT(XML, '' 
        + REPLACE(@List, @Delimiter, '') 
        + '').query('.')
    ) AS a CROSS APPLY x.nodes('i') AS y(i)
    
    
    
    /*Your total words*/
    select count(*) NumberOfWords
    from @WordsTable
    where Data is not null;
    
    /*words list*/
    select *
    from @WordsTable
    where Data is not null
    

    /from this Logic you can continue alon/

提交回复
热议问题