Line of Code Explanation

  • Hey group,

    I was reviewing a CURSOR created and ran across a line of code that makes no sense to me. I was hoping someone out there could explain it to me. Here is the line.

    SET @startingIndex= Patindex('%[^0-9]%', @certDur)

    What does PATINDEX do?

    What does '%[^0-9]%' do?

    Thank you 😀

  • SQL_Dba_WannaBe (1/6/2011)


    Hey group,

    I was reviewing a CURSOR created and ran across a line of code that makes no sense to me. I was hoping someone out there could explain it to me. Here is the line.

    SET @startingIndex= Patindex('%[^0-9]%', @certDur)

    What does PATINDEX do?

    What does '%[^0-9]%' do?

    Thank you 😀

    PATINDEX

    Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types.

    The "%" is a wild card - the first one means 0-n characters before (aka the beginning of the string); while the second one means 0-n characters after (aka the end of the string).

    The [] annotates a set of characters.

    The ^ means not in the set.

    The 0-9 means the digits 0-9.

    All together, it means the starting position of the first non-numeric character in the string.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Perfect explanation. Thank you

  • I'm glad I didn't stir up the mud in the water!

    Now... do you need help in getting rid of that c.u.r.s.o.r.?

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

Viewing 4 posts - 1 through 3 (of 3 total)

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