Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Parse a fixed length numeric value from a string Expand / Collapse
Posted Thursday, September 27, 2007 3:04 PM


Group: General Forum Members
Last Login: Tuesday, July 14, 2015 10:11 AM
Points: 12, Visits: 23
Comments posted to this topic are about the item Parse a fixed length numeric value from a string
Post #403785
Posted Tuesday, January 24, 2012 5:32 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, July 2, 2014 2:48 AM
Points: 2, 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, '-', ';'), '$', ';'), ' ', ';'), ',', ';'), '.', ';'), '+', ';'), '\', ';')


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.
Post #1241297
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse