Solved

Trimming spaces from a field

Posted on 1998-08-21
6
208 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 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