?
Solved

SQL extract number of a string

Posted on 2013-01-17
5
Medium Priority
?
669 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to setup several different housekeeping processes for a 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.
Suggested Courses

762 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