Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Basic query to remove punctuation in table field

Author  Topic 

jetcityaaron
Starting Member

4 Posts

Posted - 2011-10-05 : 18:17:50
I'm new to SQL and I need help with a very basic query to remove punctuation from one of our database tables.

Basically, we have a table called PATIENTS and in this table there is a field called LAST NAME where at the end of every patients last name there is a *

I need to remove the * for 5000+ last names and I want to write a query to do this. I think I'm close but the query I'm writing is isn't working. This is what I have....

Select * from PATIENT where LAST NAME like '*'

I thought that query would remove all the punctuation with the * in that field but it didn't. What am I doing wrong?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-10-05 : 18:28:55
update PATIENTS
set [last name] = REPLACE([last name], '*', '')
where [last name] like '%*'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jetcityaaron
Starting Member

4 Posts

Posted - 2011-10-05 : 18:36:07
Awesome! That worked.

Thanks Tara :)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-10-05 : 18:48:40
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jetcityaaron
Starting Member

4 Posts

Posted - 2011-10-10 : 17:36:28
The query worked pretty good but I still have a couple hundred last names with a *

It appears the query remove all the *'s where the * was at the end of the last name.

In this case I have *'s right in the middle of the last name
How would I remove the * from a last name that is spelled SM*TH ?

Go to Top of Page

jassi.singh
Posting Yak Master

122 Posts

Posted - 2011-10-11 : 01:12:11
update PATIENTS
set [last name] = REPLACE([last name], '*', '')
where [last name] like '%*%'

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh
Go to Top of Page

Cindyaz
Yak Posting Veteran

73 Posts

Posted - 2011-10-11 : 04:18:23
Use the same query posted by Tara Kizer without the where clause. This will replace all * with '' from the last name.

Go to Top of Page

jetcityaaron
Starting Member

4 Posts

Posted - 2011-10-11 : 19:25:14
Ahhh - just one % was all I needed.
I thought I tried that earlier but I just copied\pasted that in and it worked.

Thanks for the replies Jassi, Tara and Cindyaz.
Go to Top of Page
   

- Advertisement -