Link to home
Start Free TrialLog in
Avatar of BrookK
BrookK

asked on

Parse numbers

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

Avatar of wdosanjos
wdosanjos
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of waltersnowslinarnold
waltersnowslinarnold
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BrookK
BrookK

ASKER

answer was close to what I was looking for