Include ampersand as part of a string in a SQL query

Posted on 2013-09-02
Medium Priority
Last Modified: 2013-09-07
I have the following query
Select NgoName As "NGO",  
    dbo.FormatToIndianCurrency(TotalRaised) As "Total Raised"    
      From tblNgoDailyReciepts2013 tnd with (nolock)  
      inner join manufacturer mfi with (nolock) on tnd.NgoName=mfi.Name  
where mfi.name  in (@Ngolist)  
      Order By TotalRaised desc  
I am passing a set of comma separated string  as part of the @Ngolist parameter. The query seems to work fine as long as there is no & as part of the  @Ngolist   so for example it works fine with   mfi.name  in ('A','B') . However if I have an ampersand the results are not what are expected.
So for example if I have  mfi.name  in ('A&Company','B') it does not produce the correct result.

Any ideas on how I can go about fixing this ?

Question by:giveindia
LVL 20

Accepted Solution

dsacker earned 750 total points
ID: 39458761
A long string usually cannot serve an IN list. Either put all of the possible values in a temporary table and change it to:

where mfi.name in (select name from @MyTempTable)
or use the charindex function:

where charindex(mfi.name, @Ngolist) > 0
The ampersand may not be the cause of the problem.
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 750 total points
ID: 39458765

Expert Comment

ID: 39458843
mfi.name  in (replace('A&Company, '&', ' ')','B')

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this article, we will see two different methods to recover deleted data. The first option will be using the transaction log to identify the operation and restore it in a specified section of the transaction log. The second option is simpler and c…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

607 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