Solved

Trimming spaces from a field

Posted on 1998-08-21
6
211 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 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