Click here to Skip to main content
15,891,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
UPDATE tbl SET Colname= '6277',Colname= '8/16/2017 2:39:17 PM',Where mrddmonth_review_id =448



Want to remove comma first where

What I have tried:

UPDATE tbl SET Colname= '6277',Colname= '8/16/2017 2:39:17 PM',Where mrddmonth_review_id =448
Posted
Updated 29-Apr-18 7:59am
v2
Comments
Tomas Takac 16-Aug-17 7:39am    
You need to show the code that produced this string. Please use Improve question to update your question. Your code should go into the "What I have tried" section.
Jochen Arndt 16-Aug-17 7:53am    
You have tagged your question with C#.

If you have a C# String object, use the Replace(Char, Char) function with ',' and ' ' (space).

But even then you will have an invalid SQL command because you are trying to set the Colname field twice.
Dave Kreskowiak 16-Aug-17 10:20am    
While the "solutions" are answering the direct question, the real problem is why do you have a comma there in the first place? Fix that problem and you won't have to "patch it" out as a final operation.

C#
sqlStatement = sqlStatement.Replace(",Where", " Where");


Just replace ",WHERE" with " WHERE". Pretty simple.
 
Share this answer
 
Comments
Prifti Constantine 18-Aug-17 4:48am    
This answer seems pretty specific when it comes to removing commas since it will only remove the comma that exists before the "WHERE"...
My solution seems far better in terms of dynamic functionality and nevertheless this answer got a better result than mine... pretty low to downvote to get first place solution :) ... since my answer is far better than yours...
ZurdoDev 18-Aug-17 7:44am    
Actually, your solution is wrong and should be downvoted, even though I did not. But I might now because I already left a comment for you on how to fix your response and instead of fixing your bad code you came after me.
One simple solutions would be to replace the comma (',') with empty char if your string variable contains the (',') in the first place:
C#
string sqlStatement = "<pre>UPDATE tbl SET Colname= '6277',Colname= '8/16/2017 2:39:17 PM',Where mrddmonth_review_id =448";
                if (sqlStatement.Contains(',')) {
                    sqlStatement = sqlStatement.Replace(',', ' ');
                }


Another solution would be to use the
C#
sqlStatement.Remove()
method that string variables provide by themselves...
 
Share this answer
 
Comments
ZurdoDev 16-Aug-17 9:14am    
So, this will make the sql statement become
UPDATE tbl SET Colname= '6277' Colname= '8/16/2017' WHERE
which won't work.
Prifti Constantine 16-Aug-17 9:41am    
This is just an if statement that checks the string everytime if it has any commas inside of it... You can put this code inside a method or an event and trigger it through some sort of way(many exist buttons,TextBox text change etc...). By this everytime this specific block of code will be executed and it will remove the commas from your string. I hope i was fully understood and that this will help you out!
Dave Kreskowiak 16-Aug-17 10:19am    
The better solution would be to not put the comma in the string in the first place.

Since we have no idea how this string is generated, it's impossible to tell him how to fix it.
Prifti Constantine 16-Aug-17 10:30am    
Of course you can tell him how to format it... Everything is possible.. The code i provided, no matter what type of string it is, it will always remove commas from a string. It does not matter if it is an sqlStatement string or a simple "hello," string.....It will always remove the commas . Since the string is generated then the only thing certain is that you can actually get a hold of the string ,put it in a variable and act on it as you wish...Impossibilities exist for those who wish to quit.

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