Using PATINDEX to find varying length patterns in T-SQL

后端 未结 6 2137
旧时难觅i
旧时难觅i 2020-12-19 01:20

I\'m looking to pull floats out of some varchars, using PATINDEX() to spot them. I know in each varchar string, I\'m only interested in the first float that exists, but they

6条回答
  •  佛祖请我去吃肉
    2020-12-19 02:15

    I blogged about this a while ago. Extracting numbers with SQL server

    Declare @Temp Table(Data VarChar(100))
    
    Insert Into @Temp Values('some text 456.09 other text')
    Insert Into @Temp Values('even more text 98273.453 la la la')
    Insert Into @Temp Values('There are no numbers in this one')
    
    Select Left(
                 SubString(Data, PatIndex('%[0-9.-]%', Data), 8000),
                 PatIndex('%[^0-9.-]%', SubString(Data, PatIndex('%[0-9.-]%', Data), 8000) + 'X')-1)
    From   @Temp
    

提交回复
热议问题