Search code examples
sqlsql-server-2008patindex

Patindex in SQL Server 2008 R2


I am trying to use the PATINDEX function in SQL Server 2008 R2 to extract the value 3 from the string

Charged Hourly Fee for 3 CR for BCP202DL Personal Development II

but I seem to be making a mistake.

I tried

SELECT PatIndex('%[0-9]%', 'Charged Hourly Fee for 3 CR for BCP202DL Personal Development II')

which returns the position 24 yet I want the value 3.

Could someone assist with the solution?


Solution

  • Please try:

    Select substring(Data, PatIndex('%[0-9]%', Data), 1)
    from(
        select 'Charged Hourly Fee for 3 CR for BCP202DL Personal Development II' as Data
    )x