Click here to Skip to main content
14,929,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I am using SQL SERVER 2008,I have to remove last "OR" word from query in SQL SERVER 2008.
my sample query is--select ID from Emp where city='abc' OR pincode=123456 OR 

I am building dynamically query therefore extra OR is coming.

Please help me to remove this extra OR from query using function(SQL SERVER 2008)
Posted
Comments
Philippe Mori 30-Aug-11 8:39am
   
You should not add it (the extra OR) to start with...

SQL
SET @MyString = SUBSTRING(@MyString, 1, LEN(@MyString)-2)
   
Try:
C#
string query = "select ID from Emp where city='abc' OR pincode=123456 OR ";
int index = query.LastIndexOf("OR");
if (index >= 0)
   {
   query = query.Substring(0, index);
   }
   
Comments
Pravinjas 30-Aug-11 8:24am
   
I have to do this in SQL SERVER 2008(function)
Simply properly build your query without the final OR when you build it!

Typically, I would recommand you to build your query in C# as it is probably easier to do and also validate the query.

Anyway, remember that you should never build a query from string specified by the user as it open security holes.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900