Trimming spaces from a field

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 ?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

mayhewConnect With a Mentor Commented:
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.  :)

jvh042097Author Commented:
Edited text of question
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.
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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

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 ...

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.