Solved

SQL extract number of a string

Posted on 2013-01-17
5
627 Views
Last Modified: 2013-02-22
Hello

I use this function to exctract number out of a string, it worked great until somebody put the
word 'Inc.'  instead of a number the '.' created a problem and generate an error:

declare @param varchar(50)
select Left(SubString(@param, PatIndex('%[0-9.-]%', @param), 8000), PatIndex('%[^0-9.-]%', SubString(@param, PatIndex('%[0-9.-]%', @param), 8000) + 'X')-1)

Thanks for the help
0
Comment
Question by:arnololo123
5 Comments
 
LVL 3

Expert Comment

by:LordKnightshade
ID: 38789843
If you only need to worry about accounting for the period just wrap the SELECT value in a REPLACE targeting the period.  It's not particularly elegant, but it works :)

declare @param varchar(50)
select @param = 'Inc.'
select REPLACE(Left(SubString(@param, PatIndex('%[0-9.-]%', @param), 8000),PatIndex('%[^0-9.-]%',
	SubString(@param, PatIndex('%[0-9.-]%', @param), 8000) + 'X')-1),'.','')

Open in new window

0
 

Author Comment

by:arnololo123
ID: 38790013
Well the problem with this approach is that the value 3.25 for example would be stripped of the period.
0
 
LVL 3

Expert Comment

by:LordKnightshade
ID: 38793076
Is it feasible for you to use a second variable in the function?  If so you can cut down on the code by evaluating a variable after the evaluation.

declare @param varchar(50), @param2 varchar(50)
select @param = '3.25'
select @param2 = Left(SubString(@param, PatIndex('%[0-9.-]%', @param), 8000),PatIndex('%[^0-9.-]%',
	SubString(@param, PatIndex('%[0-9.-]%', @param), 8000) + 'X')-1)
select (CASE @param2 WHEN '.' THEN '' ELSE @param2 END)

Open in new window

0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 38793825
declare @param varchar(50)
select Left(SubString(@param, PatIndex('%[^a-z][0-9.-]%', @param), 8000), PatIndex('%[^0-9.-]%', SubString(@param, PatIndex('%[^a-z][0-9.-]%', @param), 8000) + 'X')-1)
0
 
LVL 9

Expert Comment

by:mimran18
ID: 38822112
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question