Extracting numbers with SQL Server

1

DECLARE @NumStr varchar(1000)
SET @NumStr = ‘This string contains 5.0,9 words and now 9 words’;

BEGIN
WHILE PATINDEX(‘%[^0-9]%’,@NumStr)> 0
SET @NumStr = REPLACE(@NumStr,SUBSTRING(@NumStr,PATINDEX(‘%[^0-9]%’,@NumStr),1),”)
END

PRINT @NumStr

2

We all have perfectly normalized tables, with perfectly scrubbed data, right? I wish! Sometimes we are stuck with dirty data in legacy applications. What’s worse is that we are sometimes expected to do interesting things with dirty data. In this blog, I will show you how to extract a number from a varchar column that contains letters and numbers.

First, let’s take a look at what some of that data might look like:

2.1 miles
4 miles
Approximately 6.5 miles
3.9
7.2miles

Suppose we wanted to extract just the numeric portion of the text. How would we do this? My first reaction was to use PatIndex to find the first non-numeric character. Unfortunately, this won’t work because of the 3rd row (Approximately 6.5 miles). Then, I thought about CharIndex, knowing there is an optional 3rd parameter that allows us to pick the starting location for the search. Unfortunately, CharIndex doesn’t allow pattern matching, and PatIndex doesn’t accommodate starting somewhere other than the beginning.

How do we do this? Well… it get’s tricky.

The first thing we need to do is to find the character that is a number. For this, we can use PatIndex:

Select PatIndex(‘%[0-9.-]%’, Data)

Next, Let’s remove any characters from the beginning of the string, like this:

Select SubString(Data, PatIndex(‘%[0-9.-]%’, Data), 8000)

This allows us to accommodate any characters that appear before the numbers. The substring result forces the numbers to the beginning of the string. Next step is to determine where the numbers end. For that, we can use PatIndex again.

Select PatIndex(‘%[^0-9.-]%’, SubString(Data, PatIndex(‘%[0-9.-]%’, Data), 8000))

PatIndex returns an integer for the first character that matches. We’ll want to use this in a LEFT function, there there is a potential problem. If there’s no match, PatIndex will return 0. So, we should make sure that PatIndex will always return at least one. We can trick the system by appending a character before running the PatIndex function. Like this:

Select PatIndex(‘%[^0-9.-]%’, SubString(Data, PatIndex(‘%[0-9.-]%’, Data), 8000) + ‘X’)

By including something we know will match the pattern, we guarantee that we get a number greater than zero from the PatIndex function.

Next step is to get the left part of the substring, which will return just the numbers. Like this:

Select Left(SubString(Data, PatIndex(‘%[0-9.-]%’, Data), 8000), PatIndex(‘%[^0-9.-]%’, SubString(Data, PatIndex(‘%[0-9.-]%’, Data), 8000) + ‘X’)-1)

Now, I will admit that this is a very ugly formula to use. However, there is a high probability that it is re-usable. Because of this, it would make a nice little function to have in our SQL Arsenal.

tsql
Line number Off | Hide | Select all

1.CREATE FUNCTION dbo.GetNumbers(@DATA VARCHAR(8000))
2.RETURNS VARCHAR(8000)
3.AS
4.BEGIN
5. RETURN LEFT(
6. SUBSTRING(@DATA, PATINDEX(‘%[0-9.-]%’, @DATA), 8000),
7. PATINDEX(‘%[^0-9.-]%’, SUBSTRING(@DATA, PATINDEX(‘%[0-9.-]%’, @DATA), 8000) + ‘X’)-1)
8.END

Now, we can use this function wherever we need it. As a final step, let’s test it on our sample data. Before doing this, let’s think about other data that could potentially cause us problems. We should test an empty string, NULL, a string without any numbers, and a string that contains multiple numbers separated by characters.
Declare @Temp Table(Data VarChar(60))

tsql
Line number Off | Hide | Select all

1.INSERT INTO @Temp VALUES(‘2.1 miles’)
2.INSERT INTO @Temp VALUES(‘4 miles’)
3.INSERT INTO @Temp VALUES(‘Approximately 6.5 miles’)
4.INSERT INTO @Temp VALUES(‘3.9’)
5.INSERT INTO @Temp VALUES(‘7.2miles’)
6.INSERT INTO @Temp VALUES(”)
7.INSERT INTO @Temp VALUES(NULL)
8.INSERT INTO @Temp VALUES(‘No Numbers Here’)
9.INSERT INTO @Temp VALUES(‘approximately 2.5 miles, but less than 3’)
10.
11.SELECT DATA, dbo.GetNumbers(DATA)
12.FROM @Temp

The results are:

Data Numbers
—————————————- ——-
2.1 miles 2.1
4 miles 4
Approximately 6.5 miles 6.5
3.9 3.9
7.2miles 7.2

NULL NULL
No Numbers Here
approximately 2.5 miles, but less than 3 2.5

enjoy!!!

Advertisements