Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
This is my sample data in a column

this is ! Alex23 & and % doing/ *SQL2019

i want this output from above 

this is Alex23 and doing SQL2019

we are not allowed to use stored procedure and function in organization. we have to type so many update and replace queries, so looking for a better solution which doesn't include so many replace queries.


What I have tried:

update tablename set columnname = replace(columnname,'!','')
update tablename set columnname = replace(columnname,'@','')
update tablename set columnname = replace(columnname,'#','')
update tablename set columnname = replace(columnname,'$','')
update tablename set columnname = replace(columnname,'%','')
update tablename set columnname = replace(columnname,'&','')
Posted
Updated 11-May-24 20:44pm
v2
Comments
Wendelius 5-May-24 6:19am    
updating the row should work fine so what is the issue with that?
Member 9553610 12-May-24 2:39am    
update table works fine, but there are 30+ special characters and some non printable characters as well in the data. we have to type this much update queries. so that's why i am looking for a better and optimized query for this
Richard Deeming 13-May-24 3:50am    
Well, if you're "not allowed to use stored procedure and function in organization" (why not?), then you're not going to find a better or more optimized query.

SQL Server doesn't natively support regular expressions, and the only way to add support is using SQLCLR functions. If you're banned from using stored procedures and functions, then you're unlikely to be allowed to use SQLCLR functions.

NB: By "functions", I'm assuming you mean "user-defined functions". Because technically, Replace[^] is a function.

1 solution

If update isn't an option, you can always use nested calls to the replace function when selecting, for example
SQL
select replace(
         replace(
           replace('this% contains ! illegal/ chars',
           '!',''), 
         '%',''), 
       '/','')
 
Share this answer
 
v2
Comments
Member 9553610 12-May-24 2:41am    
there are 30+ special characters and some non printable characters as well in the data. we have to type this much update queries. it looks a bit hectic to type this much replace functions.
Wendelius 13-May-24 13:22pm    
If you don't want to use separate replace-calls, have a look at https://www.codeproject.com/Tips/5382272/Small-function-to-replace-characters-with-SQL-Serv

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