How to get the nth string in any generic word or sentence with a space delimiter

◇◆丶佛笑我妖孽 提交于 2020-01-25 04:00:07

问题


How do I get the nth word in a sentence or a set of strings with space delimiter?

Sorry for the change in the requirement.Thank you.


回答1:


By using instr.

select substr(help, 1, instr(help,' ') - 1)
  from ( select 'hello my name is...' as help
           from dual )

instr(help,' ') returns the positional index of the first occurrence of the second argument in the first, inclusive of the string you're searching for. i.e. the first occurrence of ' ' in the string 'hello my name is...' plus the space.

substr(help, 1, instr(help,' ') - 1) then takes the input string from the first character to the index indicated in instr(.... I then remove one so that the space isn't included..


For the nth occurrence just change this slightly:

instr(help,' ',1,n) is the nth occurrence of ' ' from the first character. You then need to find the positional index of the next index instr(help,' ',1,n + 1), lastly work out the difference between them so you know how far to go in your substr(.... As you're looking for the nth, when n is 1 this breaks down and you have to deal with it, like so:

select substr( help
             , decode( n
                     , 1, 1
                     , instr(help, ' ', 1, n - 1) + 1
                       )
             , decode( &1
                     , 1, instr(help, ' ', 1, n ) - 1
                     , instr(help, ' ', 1, n) - instr(help, ' ', 1, n - 1) - 1
                       )
               )
  from ( select 'hello my name is...' as help
           from dual )

This will also break down at n. As you can see this is getting ridiculous so you might want to consider using regular expressions

select regexp_substr(help, '[^[:space:]]+', 1, n )
  from ( select 'hello my name is...' as help
           from dual )



回答2:


Try this. An example of getting the 4th word:

select names from (
    select 
        regexp_substr('I want my two dollars','[^ ]+', 1, level) as names,
        rownum as nth
    from dual
    connect by regexp_substr('I want my two dollars', '[^ ]+', 1, level) is not null
)
where nth = 4;

The inner query is converting the space-delimited string into a set of rows. The outer query is grabbing the nth item from the set.




回答3:


Try something like

WITH q AS (SELECT 'ABCD EFGH IJKL' AS A_STRING FROM DUAL)
  SELECT SUBSTR(A_STRING, 1, INSTR(A_STRING, ' ')-1)
    FROM q

Share and enjoy.


And here's the solution for the revised question:

WITH q AS (SELECT 'ABCD EFGH IJKL' AS A_STRING, 3 AS OCCURRENCE FROM DUAL)
  SELECT SUBSTR(A_STRING,
                CASE
                  WHEN OCCURRENCE=1 THEN 1
                  ELSE INSTR(A_STRING, ' ', 1, OCCURRENCE-1)+1
                END,
                CASE
                  WHEN INSTR(A_STRING, ' ', 1, OCCURRENCE) = 0 THEN LENGTH(A_STRING)
                  ELSE INSTR(A_STRING, ' ', 1, OCCURRENCE) - CASE
                                                               WHEN OCCURRENCE=1 THEN 0
                                                               ELSE INSTR(A_STRING, ' ', 1, OCCURRENCE-1)
                                                             END - 1
                END)
    FROM q;

Share and enjoy.




回答4:


CREATE PROC spGetCharactersInAStrings ( @S VARCHAR(100) = '^1402 WSN NI^AMLAB^tev^e^^rtS htimS 0055518', @Char VARCHAR(100) = '8' ) AS -- exec spGetCharactersInAStrings '^1402 WSN NI^AMLAB^tev^e^^rtS htimS 0055518', '5' BEGIN DECLARE @i INT = 1, @c INT, @pos INT = 0, @NewStr VARCHAR(100), @sql NVARCHAR(100), @ParmDefinition nvarchar(500) = N'@retvalOUT int OUTPUT'

DECLARE @D TABLE
(
    ID INT IDENTITY(1, 1),
    String VARCHAR(100),
    Position INT
)

SELECT @c = LEN(@S), @NewStr = @S

WHILE @i <= @c
BEGIN
    SET @sql = ''
    SET @sql = ' SELECT @retvalOUT = CHARINDEX(''' +  + @Char + ''',''' + @NewStr + ''')'
    EXEC sp_executesql @sql, @ParmDefinition, @retvalOUT=@i OUTPUT;

    IF @i > 0
    BEGIN
        set @pos = @pos + @i
        SELECT @NewStr = SUBSTRING(@NewStr, @i + 1, LEN(@S))
        --SELECT @NewStr '@NewStr', @Char '@Char', @pos '@pos', @sql '@sql'
        --SELECT @NewStr '@NewStr', @pos '@pos'

        INSERT INTO @D
            SELECT @NewStr, @pos

        SET @i = @i + 1
    END
    ELSE
        BREAK
END
SELECT * FROM @D

END




回答5:


If you're using MySQL and cannot use the instr function that accepts four parameters or regexp_substr, you can do this way:

select substring_index(substring_index(help, ' ', 2), ' ', -1)
from (select 'hello my name is...' as help) h

Result: "my".

Replace "2" in the code above with the number of the word you want.




回答6:


If you are using SQL Server 2016+ then you can take advantage of the STRING_SPLIT function. It returns rows of string values and if you aim to get nth value, then you can use Row_Number() window function.

Here there is a little trick as you don't want to really order by something so that you have to "cheat" the row_number function and allow its value in the natural order which is the STRING_SPLIT() function will spit out.

Below is a code snippet if you want to find the third word of the string

Declare @_intPart INT = 3; -- change nth work here, start # from 1 not 0 
SELECT value FROM(
    SELECT  value, 
    ROW_NUMBER()OVER(ORDER BY (SELECT 1)) AS rowno
    FROM STRING_SPLIT('hello world this is amazing', ' ')
) AS o1 WHERE o1.rowno = @_intPart;

You can also make a scalar function to retrieve values.



来源:https://stackoverflow.com/questions/10109335/how-to-get-the-nth-string-in-any-generic-word-or-sentence-with-a-space-delimiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!