Reputation:
I want to extract a specific number value from this query...
Column: name Four rows for this example:
I want the integer value only. I want with the query:
The patterns are:
How can i use this regexp on a SQL Query to parse an attribute value?
SELECT FROM myTable
SUBSTRING(name, (PATINDEX('%[0-9]%', [name])),4) as peso
This extract some values, but not in correct order... I think that i can apply LEFT with length until integer value, but i don't know how resolve it.
Upvotes: 3
Views: 23482
Reputation: 1
Should be something like SUBSTRING(name, PATINDEX('%[0-9][0-9][0-9][0-9][G ][GR]%', name),4)
, if they always do use the same pattern. Will get some false positives ('1400 Rhinos', '3216GGG' and the like).
Upvotes: 0
Reputation:
CONVERT(
INT,
(REPLACE(
SUBSTRING(
nome,
(
PATINDEX(
'%[0-9]%',
REPLACE(
REPLACE(
nome,
'1/2',
''
),
'1/4',
''
)
)
),
4
),
'G',
''
))) as pesoTotal
This resolve the question, thanks.
Upvotes: 1
Reputation: 1269633
You are close. In SQL Server, the simplest method for your data is:
select left(name, 4) as peso
from mytable t;
That is, the first four characters seem to be what you want.
If the first four characters may not all be digits, then you want to use patindex()
:
select left(name, patindex('%[^0-9]%', name + ' ') - 1) as peso
from myTable t;
Upvotes: 2