8

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
  • Do you mean "whitespace" or "space"? The former includes tabs, and the accepted answer won't handle that. Commented Jul 15, 2013 at 8:22

6 Answers 6

16

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
Sign up to request clarification or add additional context in comments.

3 Comments

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 - +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.
@bwawok -- or a fast full index scan.
10

A quick and dirty way

WHERE LENGTH(TRIM(COL1)) <> LENGTH(COL1)

Comments

2

So why can't you use the following to find the leading spaces? I've been able to identify the records with leading spaces this way and using '% ' to find the trailing spaces.

SELECT mycolumn
FROM my_table
WHERE mycolumn LIKE ' %'

I've also used the following to remove both the leading and trailing spaces

Update My_table set Mycolumn = TRIM(Mycolumn) 

which seems to work just fine.

Comments

1

You could use regular expressions in Oracle.

Example:

select * from your_table 
 where regexp_like(your_column, '^[ ]+.*')
    or regexp_like(your_column, '.*[ ]+$')

1 Comment

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
-1
select data1, length(data1)-length(replace(data1,' ','')) from t;

Comments

-1

Following query will retrieve rows when one of Table fields T$DSCA has trailing spaces at the end: SELECT * from TABLE_NAME A WHERE RAWTOHEX(SUBSTR(A.T$DSCA, LENGTH(T$DSCA),1)) ='A0' AND TRIM(T$DSCA) is not null;

2 Comments

Consider describing your solution instead of "here's the code, goodbye."
Welcome to Stack Overflow! Although this code may help to solve the problem, it doesn't explain why and/or how it answers the question. Providing this additional context would significantly improve its long-term educational value. Please edit your answer to add explanation, including what limitations and assumptions apply.

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.