Solved

Parse numbers

Posted on 2011-02-14
3
296 Views
Last Modified: 2012-08-14
Hello All,

I have to write a function to parse numbers after dash ('-') but excluding the zeros from the strings below-
'NA3000.~1A-003'
'NA457~1A.02-5'
'NA200.~1AS'
'NP42201~1.02-02'

Result-
3
5
0 -if no dash
2

This is what I have done so far-
Can anyone help me?
declare @delimiter varchar(10), @string varchar(100), @string1 varchar(100)
select @delimiter = '-', @string = 'KP008~1.003-002'

select @string1 = ISNULL(SUBSTRING(@string, CHARINDEX(@Delimiter, @string)+1, LEN(@string)), 0)

Open in new window

0
Comment
Question by:BrookK
3 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34888824
I think you just need to add a CAST and you should be good:

declare @delimiter varchar(10), @string varchar(100), @string1 varchar(100)
select @delimiter = '-', @string = 'KP008~1.003-002'

select @string1 = CAST(ISNULL(SUBSTRING(@string, CHARINDEX(@Delimiter, @string)+1, LEN(@string)), 0) as int)
select @string1

Open in new window

0
 
LVL 7

Accepted Solution

by:
waltersnowslinarnold earned 500 total points
ID: 34888854
Use the following sample. it will help.
declare @delimiter varchar(10), @string varchar(100), @string1 varchar(100)
select @delimiter = '-', @string = 'KP008~1.003-005'
IF (PATINDEX('%-%',@string) > 0)
select @string1 = REPLACE(ISNULL(SUBSTRING(@string, CHARINDEX(@Delimiter, @string)+1, LEN(@string)), 0),0,'')
ELSE
select ISNULL(@string1,0)

Open in new window

0
 

Author Closing Comment

by:BrookK
ID: 35232796
answer was close to what I was looking for
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

16 Experts available now in Live!

Get 1:1 Help Now