Solved

SQL extract number of a string

Posted on 2013-01-17
5
608 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
Comment Utility
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
Comment Utility
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
Comment Utility
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:
ScottPletcher earned 500 total points
Comment Utility
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
Comment Utility
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now