Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to match trailing whitespace

Posted on 2012-04-02
7
Medium Priority
?
350 Views
Last Modified: 2012-06-27
I have a field that I need to match the string however there is trailing white space on the end.

How do I grab everything that matches the expression including any trailing whitespace?

WHERE field = 'S-34.5X3.5M' should return the same result as WHERE filed LIKE 'S-34.5X3.5M' however due to the trailing white space the first returns a result while the second does not.

I do not want things like 'S-34.5X3.5MH' and 'S-34.5X3.5M R S  RM' returned
0
Comment
Question by:turn123
[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
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:Philippe Damerval
ID: 37798022
Hi,
You can user LTRIM and RTRIM to remove trailing and leading spaces respectively.

HTH,

Philippe
0
 
LVL 11

Author Comment

by:turn123
ID: 37798033
Thank you Philippe.

Would you mind giving me an example of how to use this in a query using LIKE to find the correct records?
0
 
LVL 9

Expert Comment

by:Philippe Damerval
ID: 37798333
Sure, although the idea here is specifically not to use LIKE (in order to avoid returning the values you don't want).

SELECT field1, field2 from MYTABLE
WHERE RTRIM(field) = 'S-34.5X3.5M'

If your database is case sensitive (off by default) then you may want to use UPPER to convert all letters to uppercase first, to ensure matches for all cases:

WHERE UPPER(RTRIM(field)) = 'S-34.5X3.5M'

HTH,

Philippe
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 11

Author Comment

by:turn123
ID: 37798513
Hi Philippe,

Specifically my problem is that I need to use LIKE. The example here had the wild card replaced to show the problem (the record exists but LIKE won't pick it up even with the EXACT SAME value as the = query as due to the trailing white space).  I apologize if I was unclear.

The actual LIKE ''%-34.5X3.5M'' where % could be quite a few different values and more values could be added in the future.

Your thoughts?
0
 
LVL 9

Accepted Solution

by:
Philippe Damerval earned 1000 total points
ID: 37798595
Have you tried

WHERE UPPER(RTRIM(field)) LIKE '%-34.5X3.5M' ?
0
 
LVL 9

Expert Comment

by:Philippe Damerval
ID: 37798604
P.S. You can use the underscore (_) character to match a single character, as well as braces ([]) to match any character within a range. More detail, and some great examples at
http://msdn.microsoft.com/en-us/library/aa933232%28v=sql.80%29.aspx

HTH,

Philippe
0
 
LVL 11

Author Closing Comment

by:turn123
ID: 37798901
Pefect thank you!
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 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