Below is my sql query :
select * from Table where col1 = @param1 and col2 = @param2
Now I want to delete everything from where clause so expected output is like below :
select * from Table;
Code:
string str = "select * from Table where col1 = @param1 and col2 = @param2";
var str = sqlQuery.Replace("where", ""); // select * from Table col1 = @param1 and col2 = @param2
One way would be to work with IndexOf
and Substring
.
var sql = "select* from Table where col1 = @param1 and col2 = @param2";
var index = sql.IndexOf("where", StringComparison.OrdinalIgnoreCase);
var newSql = sql.Substring(0, index);
You could use Trim
as well to remove the white space at the end. This would look like.
var newSql = sql.Substring(0, index).Trim();
I created below extension method with the help of answer provided by @Mighty Badaboom.
static public string Foo(this string original,string wordToFind, StringComparison stringComparision = StringComparison.OrdinalIgnoreCase)
{
var index = original.IndexOf("where", stringComparision);
return original.Substring(0, index).Trim();
}
string newSql = Foo(str, "where", StringComparison.OrdinalIgnoreCase);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.