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

x
?
Solved

Search for strings with a white space in sql

Posted on 2016-11-02
9
Medium Priority
?
49 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
[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
  • 5
  • 3
9 Comments
 
LVL 9

Expert Comment

by:Trideep Patel
ID: 41869882
What is the value in your database table is it 'abc' or ' abc' with blank?
0
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 41869883
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
ID: 41869900
Great Solution as always! Cheers!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:RIAS
ID: 41869919
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
 
LVL 52

Expert Comment

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

Author Comment

by:RIAS
ID: 41869932
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 52

Expert Comment

by:Vitor Montalvão
ID: 41869952
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
ID: 41869957
Thanks will try and be right back
0
 

Author Comment

by:RIAS
ID: 41869968
Worked like charm! Thanks
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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