Solved

Parse numbers

Posted on 2011-02-14
3
314 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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