Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trimming spaces from a field

Posted on 1998-08-21
6
Medium Priority
?
212 Views
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 ?
0
Comment
Question by:jvh042097
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Author Comment

by:jvh042097
ID: 1089687
Edited text of question
0
 
LVL 1

Expert Comment

by:brunchey
ID: 1089688
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.
0
 
LVL 4

Expert Comment

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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:mitek
ID: 1089690
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 ...

0
 
LVL 5

Accepted Solution

by:
mayhew earned 400 total points
ID: 1089691
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.  :)

Don
0
 
LVL 4

Expert Comment

by:mitek
ID: 1089692
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:

SELECT * FROM users
 WHERE SOUNDEX('vander') = CASE
         -- 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
         ELSE
           SOUNDEX(last_name)
       END

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


0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

688 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