Trimming spaces from a field

Posted on 1998-08-21
Last Modified: 2010-03-19
Basic problem is checking if certain person isn't already in a table. It could be that someone other created this person, and wrote the name slightly different. Soundex is available, but gives a big difference if there are spaces between the characters or not [SOUNDEX('Van Der') <> SOUNDEX('Vander').
So i would like to trim that column first, before using the soundex function. But no internal function is available for trimming 'inside' spaces. How should i solve this problem ?
Question by:jvh042097

Author Comment

Comment Utility
Edited text of question

Expert Comment

Comment Utility
Have you tried patindex?  It returns the starting position of the first occurrence of pattern in the specified expression, or zeros if the pattern is not found. You can use wildcard characters in pattern, as long as the wildcard character % precedes and follows pattern (except when searching for first or last characters). The expression is usually a column name. You can use this function on text, char, and varchar data.

Expert Comment

Comment Utility
PATINDEX would not help in this situation. clearly, 'vander' is not a pattern in 'van der', neither 'van der' will be found in 'vander'

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.


Expert Comment

Comment Utility
Question: do you absolutely need to do it within a single SELECT query or cursor will be ok as well ?
If it's the first, it may be really hard to do (if at all possible)
If second, it's quite feasible ...


Accepted Solution

mayhew earned 100 total points
Comment Utility
I believe what brunchey meant was to use patindex to find the space in 'van der'.

It's a little long but something like:

select substring('van der',1,patindex('% %','van der')-1) + substring('van der',patindex('% %','van der')+1,datalength('van der'))

would do the trick.  Of course this only finds one space.  Like mitek said, though, if you can use a stored proc or something, it wouldn't be tough to iterate this so it strips all spaces out.

Let us know if this helps.  :)


Expert Comment

Comment Utility
There is no way to write a generalized solution in one query.
However, here is a solution for names with 0-2 spaces in them:

         -- first space detection
         WHEN CHARINDEX(' ',last_name) > 0 THEN
           SOUNDEX(SUBSTRING(last_name,1,CHARINDEX(' ',last_name) - 1) +
           SUBSTRING(last_name,CHARINDEX(' ',last_name) + 1,DATALENGTH(last_name)))
         -- second space detection
         WHEN CHARINDEX(' ',SUBSTRING(last_name,CHARINDEX(' ',last_name),DATALENGTH(last_name))) > 0 THEN
           SOUNDEX(SUBSTRING(last_name,1,CHARINDEX(' ',last_name) - 1) +
           SUBSTRING(last_name,CHARINDEX(' ',last_name) + 1,CHARINDEX(' ',SUBSTRING(last_name,CHARINDEX(' ',last_name),DATALENGTH(last_name))) - 1) +
           SUBSTRING(last_name,CHARINDEX(' ',SUBSTRING(last_name,CHARINDEX(' ',last_name),DATALENGTH(last_name))) + 1,DATALENGTH(last_name)))
         -- no spaces detected

following the same pattern, one could write a solution for 0-3, and even for 0-4 spaces. however, if more than 3 spaces are expected, it will make sense to write a generalized solution with a cursor and a space-removing routine


Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now