In SQL Server can you find the first number in a string?

前端 未结 5 901
难免孤独
难免孤独 2021-02-12 10:12

I have a varchar(50) sql server column with data like this:

RawData
----------------------------
Washington 40 New Orleans 32
Detroit 27 St. Louis 23


        
相关标签:
5条回答
  • 2021-02-12 10:20

    Maybe a bit complicated but it works for what you need:

    declare @test table(mytext varchar(50))
    
    insert @test values('Washington 40 New Orleans 32')
    insert @test values('Detroit 27 St. Louis 23')
    
    select 
    WinningTeam=SubString(mytext, 0,PatIndex('%[0-9.-]%', mytext)), 
    WinningTeamScore=Left(SubString(mytext, PatIndex('%[0-9.-]%', mytext), 50),PatIndex('%[^0-9.-]%', SubString(mytext, PatIndex('%[0-9.-]%', mytext), 50) + 'X')-1),
    LosingTeam=SubString(mytext, PatIndex('%[0-9.-]%', mytext)+3,PatIndex('%[0-9.-]%', mytext)), 
    LosingTeamScore=reverse(Left(SubString(reverse(mytext), PatIndex('%[0-9.-]%', reverse(mytext)), 50),PatIndex('%[^0-9.-]%', SubString(reverse(mytext), PatIndex('%[0-9.-]%', reverse(mytext)), 50) + 'X')-1)) 
    from @test
    

    The query above works for scores under 100 points but you can modify it to deal with any number.

    0 讨论(0)
  • 2021-02-12 10:24

    Here is a very ugly implementation of PATINDEX() which returns the data in the multiple columns:

    SELECT rtrim(substring(RawData, 1, PATINDEX('%[0-9]%', RawData) -1)) WinningTeam,
        ltrim(rtrim(substring(RawData, PATINDEX('%[0-9]%', RawData), 2))) WinningTeamScore,
        reverse(ltrim(rtrim(substring(reverse(SUBSTRING(RawData, 1, LEN(rawdata) - (PATINDEX('%[aA-zZ]%', REVERSE(rawData)) - PATINDEX('%[0-9]%', reverse(RawData))))), 1, PATINDEX('%[0-9]%', SUBSTRING(RawData, 1, LEN(rawdata) - (PATINDEX('%[aA-zZ]%', REVERSE(rawData)) - PATINDEX('%[0-9]%', reverse(RawData))))))))) LosingTeam,
        substring(reverse(RawData), PATINDEX('%[0-9]%', reverse(RawData)), (PATINDEX('%[aA-zZ]%', REVERSE(rawData)) - PATINDEX('%[0-9]%', reverse(RawData)))) LosingTeamScore
    from yourtable
    

    See SQL Fiddle with Demo

    0 讨论(0)
  • 2021-02-12 10:24

    You can use the PATINDEX function instead of CHARINDEX, here lies the documentation ;)

    0 讨论(0)
  • 2021-02-12 10:41

    my query for selecting multi-didget numbers out of a url (ignore rows without a number and rows where the number is after the '?'

    select 
        URL,
        substring(URL,PATINDEX ('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',URL),7) as id
    from data 
    where 
        PATINDEX ('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',URL)>0 
    and PATINDEX ('%[0-9][0-9][0-9][0-9][0-9][0-9][0-9]%',URL)<charindex ('?',URL)
    
    0 讨论(0)
  • 2021-02-12 10:41

    Is there a way to identify the position of the first number in a string?

    Yes

    SELECT PATINDEX('%[0-9]%','Washington 40 New Orleans 32')
    

    PATINDEX returns 0 if the pattern can't be found or the 1 based index of the beginning of the match otherwise.

    0 讨论(0)
提交回复
热议问题