Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:38:17+00:00 2026-06-04T04:38:17+00:00

The following code is resulting in an infinite loop or really really slow execution:

  • 0

The following code is resulting in an infinite loop or really really slow execution:

CREATE FUNCTION [dbo].[CleanUriPart] 
(
    -- Add the parameters for the function here
    @DirtyUriPart nvarchar(200)
)

RETURNS nvarchar(200)
AS
BEGIN;
    -- Declare the return variable here
    DECLARE @Result nvarchar(200);

DECLARE @i int;

SET @i = 1;

WHILE 1 = 1
BEGIN;
    SET @i = PATINDEX('%[^a-zA-Z0-9.~_-]%', @DirtyUriPart COLLATE Latin1_General_BIN);
    IF @i > 0
        SET @DirtyUriPart = STUFF(@DirtyUriPart, @i, 1, '-');
    ELSE
        BREAK;
END;

-- Add the T-SQL statements to compute the return value here
SELECT @Result = @DirtyUriPart;

-- Return the result of the function
RETURN @Result;

END;

The input/output should be as follows:

  • ‘abcdef’ -> ‘abcdef’ works ok
  • ‘abc-def’ -> ‘abc-def’ results in infinite loop
  • ‘abc*def’ -> ‘abc-def’ results in infinite loop
  • etc.

Please help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:38:19+00:00Added an answer on June 4, 2026 at 4:38 am
    SELECT PATINDEX('%[^a-]%', N'aaa-def' COLLATE Latin1_General_BIN),
           PATINDEX('%[^-a]%', N'aaa-def' COLLATE Latin1_General_BIN), 
           PATINDEX('%[^a-]%', 'aaa-def' COLLATE Latin1_General_BIN),
           PATINDEX('%[^-a]%', 'aaa-def' COLLATE Latin1_General_BIN)
    

    Returns

    ----------- ----------- ----------- -----------
    1           5           5           5
    

    So it seems that for varchar datatypes a trailing - is treated as being part of a set whereas for nvarchar it is ignored (treated as a malformed range as a is ignored too?)

    The BOL entry for LIKE doesn’t explicitly talk about how to use - within [] to get it to be treated as part of a set but does have the example

    LIKE '[-acdf]'
    

    to match -, a, c, d, or f so I assume that it needs to be the first item in a set (i.e. that [^a-zA-Z0-9.~_-] needs to be altered to [^-a-zA-Z0-9.~_]). That also matches the result of my testing above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is the following code resulting in Segmentation fault? (I'm trying to create two
The following extract of code is failing to compile resulting in not code paths
following code doesn't work with input: 2 7 add Elly 0888424242 add Elly 0883666666
Running the following code resulting in an HostException; Public Sub RunPowershellInConsole(ByVal scriptText As String)
This is to do with the following code, which uses a for loop to
I use the following code to turn color image to grayscale image. The resulting
I have the following code in my index.php page: // Write the resulting JSON
I'm capturing the window.onbeforeunload event using the following code: window.onbeforeunload = function (evt) {
I'm getting some strange values for the width/height in the following code, resulting in
The following code: var ui = TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(() => { listBox1.Items.Add(Starting to crawl +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.