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 8470245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:27:49+00:00 2026-06-10T16:27:49+00:00

I encountered a bit of code within the book I’ve been reading that has

  • 0

I encountered a bit of code within the book I’ve been reading that has me questioning the SUBSTRING() function’s behavior. The code is supposed to search a NYSIIS Replacement table (phonetic encoding example) and replace the middle ‘N-gram’ of an input string based on the location ‘End’ ‘Mid’ or ‘Start’ in the table. an excerpt is provided below:

NYSIIS Replacement Table:

Location NGram Replacement

Mid      A     A
Mid      AW    AA
Mid      E     A
Mid      EV    AF
Mid      EW    AA
Mid      I     A
USE [AdventureWorks]

DECLARE @Result NVARCHAR(100) = N'NEVADA';

DECLARE @Replacement NVARCHAR(10);

DECLARE @i INT;

SET @i = 1;

WHILE @i <= LEN (@Result)

BEGIN

    SET @Replacement = NULL;

    -- Grab the middle-of-name replacement n-gram

    SELECT TOP(1)  @Replacement = Replacement                   
    FROM dbo.NYSIIS_Replacements                         
    WHERE Location = N'Mid'
        AND SUBSTRING(@Result, @i, LEN(NGram)) = NGram
    ORDER BY LEN(NGram) DESC;


    SET @Replacement = COALESCE(@Replacement, SUBSTRING(@Result, @i, 1));   


    -- If we found a replacement, apply it

    SET @Result = STUFF(@Result, @i, LEN(@Replacement), @Replacement) 

    -- Move on to the next n-gram

    SET @i = @i + COALESCE(LEN(@Replacement), 1);


END;

SELECT @Result;

When the SUBSTRING() function encounters 2 possible matches using ‘NEVADA’ as an example (‘E’ and ‘EV’ in the table) how does it ‘know’ to use the 2 letter string as opposed to the one? Is this the expected behavior for SUBSTRING()?

I would assume the @Replacement variable would contain both ‘A’ and ‘AF’ but when debugging it only appears to contain ‘N’ in the first iteration and ‘AF’ in the second.

Also I could not understand why TOP and ORDER BY were included in this example. Commenting them out produces the same results.

  • 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-10T16:27:51+00:00Added an answer on June 10, 2026 at 4:27 pm

    The ORDER BY clause uses the length of the pattern and sorts in descending order, hence the longest match will occur first. The TOP clause limits the results to the first row. Removing the ORDER BY clause makes the result unpredictable.

    COALESCE is used to set @Replacement to either the replacement pattern, or the character at position @i in the @Result string if no pattern match was found.

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

Sidebar

Related Questions

I am reading On lisp and encountered this code (I simplified it a bit).
I've been messing around with this bit a javascript that I wrote from the
I have the following bit of code: (function(){ ... var n = n ||
I've inherited a long term maintenance code base that has two radically different iPhone
I want to run a bit of code every 20 seconds. That works fine.
I'm attempting to write an ActiveRecord-esque bit of code in Obj-C, and encountered the
In an earlier question (http://bit.ly/KNMN89), I encountered an issue caused by IE7's broken implementation
I encountered an odd problem when migrating an HTML page I'd been developing in
recently I've encountered a problem with IE. I have a function function() { ShowProgress();
I've got a bit of a complex scenario that I'm stuck on trying to

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.