Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

declare @str nvarchar (max)
set @str='Charged Hourly Fee for 3 CR for BCP202DL Personal Development II'
Select substring(@str, PatIndex('%[0-9]%', @str), 1)

this will return first digit in the string.

The way you were doing was returning the position of the first digit, and my code will return value at that position.

If you must use the patindex function and the description of your item will never contain another number such as "3" you can use the following:

select patindex('%3%', 'Charged Hourly Fee for 3 CR for BCP202DL Personal Development II')

This will return 24.

I suspect that you are looking at a string where the quantity ordered is always in the same position. In that case, I would use the substring function.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top