Parse a fixed length numeric value from a string

  • Comments posted to this topic are about the item Parse a fixed length numeric value from a string

  • 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.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply