开发者

Oracle -- finding values with leading or trailing spaces

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开发者_如何学Go either before or after the actual data.


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


A quick and dirty way

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


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.


You could use regular expressions in Oracle.

Example:

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


select data1, length(data1)-length(replace(data1,' ','')) from t;


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;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜