Solved

SQL extract number of a string

Posted on 2013-01-17
5
612 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:
ScottPletcher 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Error in WHERE Clause 5 38
ASP.NET 5 Templates 2 65
SQL Query stumper 3 36
Azure SQL Insert not working suddenly 11 19
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

914 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

14 Experts available now in Live!

Get 1:1 Help Now