16

I have 100's of cells in our database which contain ? instead of '. It is possible that this might happen in all rows and columns and in more than one word per cell. Here is an example of just one cell.

Parents? CUI assumed equal to the sum of the father?s/stepfather?s and mother?s/ stepmother?s income .

I want to write an SQL statement which finds all the cells that contain ? (may be more than one per cell) and replace them with '. I am sure that all ? have to be replaced without exception.

I know there is a function replace but I couldn't know how to extract a character from a string in sql.

This is one example I got but it couldn't help me.

UPDATE dbo.authors

SET    city = replace(city, 'Salt', 'Olympic')
WHERE  city LIKE 'Salt%';

Any ideas?

2

4 Answers 4

18
UPDATE databaseName.tableName
SET columnName = replace(columnName, '?', '''')
WHERE columnName LIKE '%?%'
Sign up to request clarification or add additional context in comments.

Comments

17

Are you sure that the data stored in the database is actually a question mark? I would tend to suspect from the sample data that the problem is one of character set conversion where ? is being used as the replacement character when the character can't be represented in the client character set. Possibly, the database is actually storing Microsoft "smart quote" characters rather than simple apostrophes.

What does the DUMP function show is actually stored in the database?

SELECT column_name,
       dump(column_name,1016)
  FROM your_table
 WHERE <<predicate that returns just the sample data you posted>>

What application are you using to view the data? What is the client's NLS_LANG set to?

What is the database and national character set? Is the data stored in a VARCHAR2 column? Or NVARCHAR2?

SELECT parameter, value
  FROM v$nls_parameters
 WHERE parameter LIKE '%CHARACTERSET';

If all the problem characters are stored in the database as 0x19 (decimal 25), your REPLACE would need to be something like

UPDATE table_name
   SET column1 = REPLACE(column1, chr(25), q'[']'),
       column2 = REPLACE(column2, chr(25), q'[']'),
       ...
       columnN = REPLACE(columnN, chr(25), q'[']')
 WHERE INSTR(column1,chr(25)) > 0
    OR INSTR(column2,chr(25)) > 0 
    ...
    OR INSTR(columnN,chr(25)) > 0

11 Comments

That might be true. I'll look in to it. Though I have no idea what NLS_LANG is. The data is stored as VARCHAR2 Thanks
@Justin .. I found out that in the database. The character is not even visible... it is empty space. By running the dump method on the column name i found this .>> Typ=1 Len=38 CharacterSet=US7ASCII: 53,74,75,64,65,6e,74,19,73,20,53,53,4e,20,6d,61,74,63,68,2c,20,62,75,74,20,6e,6f,20,6e,61,6d,65,20,6d,61,74,63,68. What does it mean? And when it is displayed it is not even question mark but some wired character. The character contains something that looks like 0019.
@WowBow - Is that the DUMP that corresponds to the sample data you posted ("Parents? CUI assumed equal to the sum of the father?s/stepfather?s and mother?s/ stepmother?s income .")? Those two don't seem to match up.
Oh I am sorry. The data is "Students SSN match, but no name match". But as you can see i copy pasted from the database and that character is gone. I have no idea why ... it was supposed to be ,>> Student?s but the question mark is gone when I paste it here
@WowBow - OK. That makes more sense. Are the problem characters always 0x19?
|
10

This will replace all ? with ':

UPDATE dbo.authors    
SET    city = replace(city, '?', '''')
WHERE city LIKE '%?%'

If you need to update more than one column, you can either change city each time you execute to a different column name, or list the columns like so:

UPDATE dbo.authors    
SET    city = replace(city, '?', '''')
      ,columnA = replace(columnA, '?', '''')
WHERE city LIKE '%?%'
OR columnA LIKE '%?%'

4 Comments

don't we need a where clause?
@MatX - A where clause will just filter the rows so it only checks the ones which need updating. The only advantage is performance, but its a worthwhile point. I'll add it to the query
Oh ... how about if I am updating more than one column .. i.e I will have more than one SET statements. How should I write the where clause in that case because I am dealing with more than one column?
@MatX - I've updated my answer. It might be worth checking what Justin is saying first though to ensure your data actually contains ?
0

Use the REPLACE function.

eg: SELECT REPLACE ('t?es?t', '?', 'w');

Source

1 Comment

Two other answers say the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.