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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:04:06+00:00 2026-05-22T22:04:06+00:00

I have a table like: | id | lastname | firstname | | 1

  • 0

I have a table like:

| id | lastname | firstname |
|  1 | doe      | john      |
|  2 | oman     | donald    |
|  3 | o'neill  | james     |
|  4 | onackers | sharon    |

Essentially, users are going to be searching by the first letters of the last name.

I want to be able to return results that contain and don’t contain punctuation from the database. For instance, when a user searches for: on

I want to return both:
o’neill, onackers

I want someone to be able to search “o, on, oneill, o neill, etc” to get o’neill.

So the best way to do this seems to take the lastname column value and have two permutations of it searched in the WHERE clause with an OR. One where any special characters are replaced with the _ in SQL, and one where all non-alpha chars (including spaces) are gone.

I figure I can use the underscore in the SQL replace to keep the one space available.

I’m having a little trouble with the WHERE clause. I’d prefer to do this with a simple REPLACE rather than creating a regex function if possible. If that’s a no-go though, I understand:

@last_name (this is the nvarchar input)

SELECT id, lastname, firstname
FROM people
WHERE ((REPLACE(people.lastname, '[^A-Za-z]', '_') like @last_name + '%')
OR (REPLACE(people.lastnname,'[^A-Za-z ]', '') like @last_name + '%'))
ORDER BY lastname

I’m pretty sure the replace part has to be on the other side of the LIKE. I’m messing up the structure but need some help.

I am Using MSSQL Server 2005.

Thank you so much in advance.

UPDATE

It seems like I have two options:

  1. Create a regular expression function using CLR (excuse me if I’m saying this wrong, I’m new to it)
  2. Create extra columns on the table or create a new “fuzzyTable” with the cleaned up last names.

The database gets updated once a night. I have actually already begun the new table approach, as it was what I was originally going to do. However, I’m beginning to think it’s smarter to add the “fuzzy” columns to the main table and then on the nightly update to add the adjusted lastnames to the new / updated rows.

Stack Overflow: Which approach is better? User-defined REGEX function I can use in the SQL, and thus avoid extra columns? Or adding the extra column or two to the table? Or a new table?

  • 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-05-22T22:04:07+00:00Added an answer on May 22, 2026 at 10:04 pm

    Depending on how complex your scenario can get, this will be lots of work, and slow too. But there’s a more flexible approach. Consider something like this, referred to as initialTable:

    | id | lastname | firstname |
    |  1 | o'malley | josé      |
    |  2 | omállèy  | dònáld    |
    |  3 | o'neill  | jámès     |
    |  4 | onackers | sharon    |
    

    Maybe a bit much, but it illustrates the general problem. I had to implement a “fuzzy” search for our intranet website based on character data that looked very similar – there’s many accents in french or spanish names or street addresses for example.

    What I did was define a function that performed all replacements for a given string, for example (pseudocode):

    function string replacestuff(string input)
    {
      input = replace(input, "è", "e");
      input = replace(input, "é", "e");
      input = replace(input, "ò", "o");
      input = replace(input, "ó", "o");
      input = replace(input, "'", "");
      ...
      return input;
    }
    

    Using this conversion function, create a second table fuzzyTable that has the following content:

    | id | lastname | firstname |
    |  1 | omalley  | jose      |
    |  2 | omalley  | donald    |
    |  3 | oneill   | james     |
    |  4 | onackers | sharon    |
    

    Now, assume you’ll get an input string for your search of josè. This can’t be found in either table. What you’ll have to do is this:

    declare @input varchar(50)
    declare @input_mod varchar(50)
    set @input = 'josè'
    set @input_mod = replacestuff(@input)
    
    SELECT id FROM initialTable WHERE firstname like @input OR firstname like @input_mod
    UNION
    SELECT id FROM fuzzyTable WHERE firstname like @input OR firstname like @input_mod
    GROUP BY id
    

    (Of course, you’d have to add % to make LIKE work.) The key here is to modify your input search string using the replacement function; this way you’ll get a match if searching for sè against a content of sé because both come down to se when being processed by the replacement function.

    You could even do a two-level search; first check only the unmodified string against the proper table and then with the statement shown above do a fuzzy search if the user says so.

    This is a very flexible approach and can handle all sorts of stuff, like finding german letters ä, ö, ü, ß by using two-letter expressions ae, oe, ue, ss. The disadvantage is that you’ll have to keep duplicates of some data, and change those duplicates within fuzzyTable as the initialTable (or the replacement function) changes. In our current use case, the intranet database gets updated once a night, so it’s not a problem.

    EDIT

    You need to be aware that, using this, in some cases you’ll get false positives. For example, we’re using this for an employee search, and if you’ve got a dutch name spelled Hoek, you’d also find this name searching for Hök, because in german the replacement for ö would be oe. This could be solved using country-aware replacement functions, but we never took the concept this far. Depending on your input data this is more or less academic, for our use case I can’t remember anyone complaining.

    The main reason why we came up with this approach in the first place was that some of the data we had to work with was riddled with spelling errors, ie. in french many vowels were accented the wrong way around, but still we needed to deliver a result.

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

Sidebar

Related Questions

I have the following tables: Person(Id, FirstName, LastName) { (1, John, Doe), (2, Peter,
I have table in my database names User with fields: Id FirstName LastName LoginName
I have an user table like this:- guid | username | password | firstname
Say I have a table people (id, firstname, lastname) . There are two other
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table like as follows: SoftwareName Count Country Project 15 Canada Visio
I have a table like this: <table> <tfoot> <tr><td>footer</td></tr> </tfoot> <tbody> <tr><td>Body 1</td></tr> <tr><td>Body
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
If I have a table like: CREATE TABLE FRED ( recordId number(18) primary key,
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',

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.