tags:

views:

48

answers:

3

I am trying to find if a certain column requires TRIM function on it.

How can I find out if this column in a table has records that have white space either before or after the actual data.

+1  A: 

A quick and dirty way

WHERE LENGTH(TRIM(COL1)) <> LENGTH(COL1)
Martin Smith
+1  A: 

You could use regular expressions in Oracle.

Example:

select * from your_table 
 where regexp_like(your_column, '^[ ]+.*')
    or regexp_like(your_column, '.*[ ]+$')
Pablo Santa Cruz
Your example doesn't need Regex - It would just be `your_column LIKE ' %' OR your_column LIKE '% ' ` I think the Regex woul;d need to be `^[\s]+.*` to match all white space
Martin Smith
+3  A: 

You can check it using the TRIM function itself, not the most efficient but accurate:

Select *
From TableA
Where MyColumn <> TRIM(MyColumn)

Though if you're checking then turning around to trim anyway, you probably want to just do it in the first place, like this:

Select TRIM(MyColumn) as TrimmedMyColumn
From TableA
Nick Craver
yes this works. but realize this means every query is a full table scan. If doing a lot of queries, consider updating your data to not have spaces.
bwawok
@bwawok - +1 - If you're always using it like this it's tremendously better to make it a once time cost and do an update, wasn't sure of the OPs situation in this case.
Nick Craver