FInd position of a number in string by using FINDSTRING

  • how can i find the position of a numeric value by using FINDSTRING function in ssis

    see example below

    code

    -------------

    aaaaa 1aaaaaa

    bbbbb2bbbbbbb

    cccc3cccccccc

    I need output as a position of a numerica value, in this case output is

    output

    ---------

    7

    6

    5

  • I'm not 100% sure about SSIS but, from what I've read, you should be able to use "%[0-9]%" as the "search string" in FINDSTRING. Of course, you'd use 1 as the "occurance" parameter.

    Hopefully, someone who's actually done such a thing will come along and either verify or correct, either of which would be appreciated.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Jeff Moden (10/24/2011)


    I'm not 100% sure about SSIS but, from what I've read, you should be able to use "%[0-9]%" as the "search string" in FINDSTRING. Of course, you'd use 1 as the "occurance" parameter.

    Hopefully, someone who's actually done such a thing will come along and either verify or correct, either of which would be appreciated.

    You can't. It's not available. FINDSTRING does not support wildcards or ranges.

    Pass it to a synchronous transformation script object and handle it there in the data flow.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • A bit of an uglier solution, but if you want to avoid using a script component and writing a .NET script to do it, you could do something like:

    FINDSTRING([string], "0", 1) > 0 ? FINDSTRING([string], "0", 1) : (FINDSTRING([string], "1", 1) > 0 ? FINDSTRING([string], "1", 1) : (FINDSTRING([string], "2", 1) > 0 ? FINDSTRING([string], "2", 1) : (FINDSTRING([string], "3", 1) > 0 ? FINDSTRING([string], "3", 1) : (FINDSTRING([string], "4", 1) > 0 ? FINDSTRING([string], "4", 1) : (FINDSTRING([string], "5", 1) > 0 ? FINDSTRING([string], "5", 1) : (FINDSTRING([string], "6", 1) > 0 ? FINDSTRING([string], "6", 1) : (FINDSTRING([string], "7", 1) > 0 ? FINDSTRING([string], "7", 1) : (FINDSTRING([string], "8", 1) > 0 ? FINDSTRING([string], "8", 1) : FINDSTRING([string], "9", 1)))))))))

  • kramaswamy (10/25/2011)


    A bit of an uglier solution, ...

    Only a bit? 😀

    I was thinking along similar lines, but couldn't face writing out all that nesting!

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Wow! Really? Isn't there any function in SSIS that's similar to PATINDEX?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • I don't think so. There are custom components available here[/url], including more than one Regex component which looks promising, though I've never used any of them. Rather than doing that, I'd probably script it, as has already been suggested.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Yeah putting in a script component and just using .NET's regex libraries would be pretty simple

  • Kramaswamy, my biggest concern with the nested choices are if you have something like:

    aaaaaaaa93aaaaaa.

    3 gets picked up first. You'd have to test for all possibilities and pick the lowest non-zero value. It gets nasty fast when trying to build your own regex.

    Synchronous script component in SSIS is something you really want to learn to do, particularly for cases like this. Heck, string manipulation is part of the speed benefit of CLR in the SQL Engine, might as well learn it here, too.

    Jeff Moden (10/25/2011)


    Wow! Really? Isn't there any function in SSIS that's similar to PATINDEX?

    No, not in expressions. A bit weaksauce, I know.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • yeah definitely agreed Craig. Just posted that incase he's looking for something quick.

    What's this synchronous script component you speak of? Is that something new in 2008?

  • kramaswamy (10/25/2011)


    yeah definitely agreed Craig. Just posted that incase he's looking for something quick.

    What's this synchronous script component you speak of? Is that something new in 2008?

    Nope, it's in '05. It depends on the settings you use to associate the inbound and outbound result sets.

    Official docs:

    http://msdn.microsoft.com/en-us/library/ms136114.aspx

    A discussion of Synchronous vs. Asynchronous:

    http://consultingblogs.emc.com/jamiethomson/archive/2005/07/25/SSIS-Nugget_3A00_-The-difference-between-synchronous-and-asynchronous-script-components.aspx

    The key difference is if you set the SynchronousInputID in the Output properties. It changes your code a little though because you don't have to do the code to 'release' rows.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Ahh okay, that's what you meant. Alright, yeah I've done that before as well. Though interesting to see the performance gains that are mentioned in the thread on Jamie's site.

  • Evil Kraig F (10/25/2011)


    Kramaswamy, my biggest concern with the nested choices are if you have something like:

    aaaaaaaa93aaaaaa.

    3 gets picked up first. You'd have to test for all possibilities and pick the lowest non-zero value. It gets nasty fast when trying to build your own regex.

    Synchronous script component in SSIS is something you really want to learn to do, particularly for cases like this. Heck, string manipulation is part of the speed benefit of CLR in the SQL Engine, might as well learn it here, too.

    Jeff Moden (10/25/2011)


    Wow! Really? Isn't there any function in SSIS that's similar to PATINDEX?

    No, not in expressions. A bit weaksauce, I know.

    It IS "weaksauce" indeed! 🙂

    As for the string manipulations of CLR, I agree but only in some cases. Several years ago, Matt Miller and I engaged in some friendly competition and I was able to write T-SQL that would sometimes beat REGEX in CLR's. It follows the old rule that generic code is generically good and specific code for specific problems is frequently better.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Jeff Moden (10/25/2011)


    It IS "weaksauce" indeed! 🙂

    As for the string manipulations of CLR, I agree but only in some cases. Several years ago, Matt Miller and I engaged in some friendly competition and I was able to write T-SQL that would sometimes beat REGEX in CLR's. It follows the old rule that generic code is generically good and specific code for specific problems is frequently better.

    Considering how often I write CLRs... (never, btw...) I find it hard to disagree with you. 😉 However, had CLRs been available back in 7.0/2k, my guess is Patindex, contains, etc would not have been robustly developed.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Heh... then thank goodness CLR's weren't invented back then. 😀

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

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

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