views:

396

answers:

3

How can I write a SQL query to replace all occurrences of space in a table with underscore without writing individual statements for each column?

+1  A: 

UPDATE [table_name] SET [field_name] = replace([field_name],' ','_'), [field_name2] = replace([field_name2],' ','_')

However, this way you still have to sum up every column, so above answer might fit more your need

Vodde
+1 400K rows in Access shouldn't pose too much of a problem. I wonder what he wants to do if there are values with > 1 consecutive space?
Mike Woodhouse
What about single quotes?
Remou
A: 

You can use a mixture of VBA and SQL:

Dim rs As DAO.Recordset
Dim db As Database

Set db = CurrentDb

Set rs = db.OpenRecordset("SELECT * FROM TheTable")

For i = 0 To rs.Fields.Count - 1
   ''You may wish to check dbMemo as well as dbText
   If rs.Fields(i).Type = dbText Then
      ''You have to watch out for single quotes as well as spaces
      s = "UPDATE TheTable SET [" & rs.Fields(i).Name & "]  = " _
      & "Replace(Replace([" & rs.Fields(i).Name & "],""''"",""'""),"" "",""_"") " _
      & "WHERE Instr([" & rs.Fields(i).Name & "],"" "")>0 " _
      & "AND [" & rs.Fields(i).Name & "] Is Not Null"
      db.Execute s, dbFailOnError
      Debug.Print db.RecordsAffected
    End If
Next
Remou
Actually, that mixture includes the Access object model too, subtle but true. Access Database Engine SQL lacks the Replace() function and must be provided by the Access object model. If you tried to execute this from outside of the Access UI your SQL would fail with a syntax error.
onedaywhen
A: 

Sometimes you don't need a hammer (code) even if the problem looks like a nail.

If this is a one-off task and the table isn't more than a few million rows you can just open the table and do a find and replace from the Edit menu (or ctrl-h) in Access.

alt text

This is one of those handy data manipulation capabilities of Access that make it so darn useful for ad-hoc database work.

Caveats:
(1) Performance won't be great on large tables, but works well for one-off data cleanup tasks in moderate sized tables (or when you are willing to wait a few minutes for it to finish on large tables.
(2) The locks this technique will create are a bit oppressive, so this isn't advised for a DB that is in active use by a large number of users.

JohnFx
Use the find-and-replace moves row-by-row through the data, which is going to be orders of magnitude slower than using a SQL UPDATE statement. -1 for advice that isn't viable for anything but tables of a few thousand rows or fewer.
David-W-Fenton
The OP specifically states that he only has a few thousand rows AND my answer specifically includes the disclaimer that it is only to be used for smaller tables. What more do you want, David?
JohnFx
Also, if it is a one off (as I also mentioned) then the performance is probably not a major concern. I've done this on tables with millions of rows. I will add one additional caveat though, the locking this does on the table is pretty brutal, so I'd avoid it in situations where there are a lot of other users. It works like a charm in data analysis type projects though.
JohnFx