SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Parse a fixed length numeric value from a string


Parse a fixed length numeric value from a string

Author
Message
Glen Potter
Glen Potter
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 23
Comments posted to this topic are about the item Parse a fixed length numeric value from a string
MikeTeeVee
MikeTeeVee
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 192
Very cool. This will come in handy for me. I want to filter out a number users entered into a textbox that allowed free-flowing text for additional notes. The number is always 8 or 9 significant digits, so this is perfect for me.

A note of caution, the following will also return true:
select ISNUMERIC('-$ ,.'), ISNUMERIC('+\')

What I would suggest is to update your function to replace these symbols (including the space) with a character that cannot be a number, like a semicolon, before parsing it.

Adding this to the beginning of the function should do the trick:
SET @String = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@String, '-', ';'), '$', ';'), ' ', ';'), ',', ';'), '.', ';'), '+', ';'), '\', ';')

OR

You could replace this Line:
if isnumeric(Substring(@String, @intCurrPos,1))=1
With this line:
if Substring(@String, @intCurrPos,1) in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')

I, personally, would go with the latter. I've had better luck with parsing using it.

Otherwise, without these changes, strings like this won't work (because the comma is counted as part of the number):
select fn_ParseStringNumberInt('Looky# 123456789, Here# 12345', 9)

Other than that, this is pretty nifty. Thank you for sharing.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search