Patindex in SQL Server 2008 R2

James Obuhuma picture James Obuhuma · Mar 20, 2013 · Viewed 13.9k times · Source

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?

Answer

TechDo picture TechDo · Mar 20, 2013

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