Solved

Search for strings with a white space in sql

Posted on 2016-11-02
9
26 Views
Last Modified: 2016-11-02
Hello,

I have a stored procedure to search a string but, it fails when it has white spaces at the beginning of it.
Example: 'abc'  searches perfectly but, ' abc' fails. Cannot use like in the query.
The query I have is
  SET @SQL = @SQL + '  AND Auth = ''' +  CAST(@Auth AS NVARCHAR(50))+ ''''	 

Open in new window


Regards
0
Comment
Question by:RIAS
  • 5
  • 3
9 Comments
 
LVL 9

Expert Comment

by:Trideep Patel
Comment Utility
What is the value in your database table is it 'abc' or ' abc' with blank?
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
Use LTRIM to remove the spaces from the beginning of the string. You can also use RTRIM if you want to remove the spaces at the end of the string. So, the following will remove spaces from begin and end of the string:
SET @SQL = @SQL + '  AND Auth = ''' +  LTRIM(RTRIM(CAST(@Auth AS NVARCHAR(50)))) + ''''	 

Open in new window

0
 

Author Closing Comment

by:RIAS
Comment Utility
Great Solution as always! Cheers!
0
 

Author Comment

by:RIAS
Comment Utility
Vitor,
Sorry for the confusion.
The data in the table is with space  example ' abc'
So need to search that.  Your solution trims the string for search but the data is with space

Cheers
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Do you mean when @Auth = ' abc' (with leading space) your original query doesn't work?
0
 

Author Comment

by:RIAS
Comment Utility
The data in the table is ' abc'.
my original query was
SET @SQL = @SQL + '  AND Auth = ''' +  CAST(@Auth AS NVARCHAR(50))+ ''''	 

Open in new window


was not able to give result.
It is just one of them as cannot change data in table.
Can I  have a query which searches even the ones with leading spaces
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Ok. It's the data in the table that has the space and not the variable. So use my above solution but on the column name. Mind that this can reduce the query performance if Auth is indexed:
SET @SQL = @SQL + '  AND LTRIM(RTRIM(Auth)) = ''' +  CAST(@Auth AS NVARCHAR(50))+ ''''	 

Open in new window

1
 

Author Comment

by:RIAS
Comment Utility
Thanks will try and be right back
0
 

Author Comment

by:RIAS
Comment Utility
Worked like charm! Thanks
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now