We help IT Professionals succeed at work.

Query to find string only containing certain characters

Hi Experts,

I am trying to clean up a data import, and want to find all records where, in a particular field they only contain spaces and commas in any combination.  

i.e.
" , " would be found
"smith, john" would not
"    , " would be found
",     ," would be found

The numbers of comma and spaces vary, and records with other data may contain commas and spaces but should be left.

Thanks.
Comment
Watch Question

BRONZE EXPERT
Top Expert 2016

Commented:
select * from tablex
where instr([filedName]," , ")>0 or  instr([filedName],"  , ")>0 or  instr([filedName],",    ,")>0
BRONZE EXPERT
Top Expert 2011
Commented:
use replace...

where len(replace(replace(column,' ',.''),',',''))=0
BRONZE EXPERT
Top Expert 2016

Commented:
<I am trying to clean up a data import, and want to find all records>
how do want to treat the records found?

as Lowfatspread stated you can use the replace function, but you should be very careful using this (save a copy of table first).

from your example,
how do you want to clean the records with  ",     ," ?

Author

Commented:
Capricorn1 - pretty close!

"Jones , Sara Jane" and a couple of others still slipped through.   The trouble is that the number of spaces and positioning are different in the entries.

Norb.

Author

Commented:
All these entries are just going to be nulled btw
Top Expert 2008
Commented:
did you try the recommendation of lowfatspread? i think that should work, except that he had an extra '.' in the statement.
where replace(replace(column,' ',''),',','')=0

Open in new window

BRONZE EXPERT
Top Expert 2016

Commented:
Norbert2000,
to be able to do it in one pass, you have to know all the combinations.

<All these entries are just going to be nulled btw> as in erased?

create a backup copy first

run this query

Update tableX
set fieldName="" where instr([fieldName]," , ")>0 or  instr([fieldName],"  , ")>0 or  instr([fieldName],",    ,")>0 or  instr([fieldName],"  ,  ")>0 or  instr([fieldName],"   , ")>0

Author

Commented:
Thanks - I don't know all combinations so this solution is great.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.