Oracle -- finding values with leading or trailing spaces - sql

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.

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;

Related

Removing leading zeroes in a select* SQL

I want to strip the leading zeroes in certain columns of my select statement. I know how to do this if the column names are listed:
For example, in
SELECT
a,b,c.... if I want to trim column b, I simply do
SELECT a, TRIM(LEADING '0' FROM b) new name, c....
Now I also want to do the same for a SELECT* statement..
Suppose I have SELECT *, and I want to trim the leading zeroes for column b only. Is there an alternative to go as to convert the SELECT * to a normal select by listing out all columns? It becomes tedious this way.
This has been answered here: Removing leading zeros from varchar sql developer
select ltrim('000012345', '0') from dual;
LTRIM
-----
12345

SQL - find rows with values that has 2 spaces or more in between

I have sql table with name and surname. Surname is in own column. The problem is with users with two surnames, because sometimes they add more than one space between surnames and then I have to find and fix them manualy.
How to find these surnames with more than one space in between?
If you want to find records which have more than one space then you can use the following trick:
SELECT surname
FROM yourTable
WHERE LENGTH(REPLACE(surname, ' ', '')) < LENGTH(surname) - 1
This query will detect two or more spaces in the surname column. If you want to also do an UPDATE this is possible, but it would be fairly database-specific, and you did not specify your database as of the time I wrote this answer.
First remove those extra spaces. Then add a constraint that makes sure it doesn't happen again:
alter table tablename add constraint surname_verify check (surname not like '% %')
(Or, even better, have a trigger making sure the surnames are properly spaced, cased etc.)
How to remove extra spaces? Depends on the dbms.
You can perhaps do something like:
update tablename set surname = replace(surname, ' ', ' ')
where surname like '% %'
The where clause isn't needed, but makes the transaction much smaller.
(Iterate to get rid of triple or more spaces.) Or use regexp_replace.
Even tidier:
select string = replace(replace(replace(' select single spaces',' ','<>'),'><',''),'<>',' ')
Output:
select single spaces

Using 'ilike' to select rows with backslash character [PostgreSQL]

I try to execute such query:
select * from my_table where table_name ilike 'PDK\1100090'
this query returns 0 rows, but in fact that table contains a row with such string. I verified that by selecting everything from that table. I also used '=' instead of ilike, and tried esaping the string with E'PDK\1100090' with no luck. I assume there is a problem with a backslash in the string but I could be wrong about that.
Thanks in advance
You claim to have tried double back slashes. Try this:
where table_name ilike 'PDK_1100090'
Does that return anything? (The underscore should match any character in that position.)
Then try for any sequence:
where table_name ilike 'PDK%1100090'
Then look for unusual characters at the beginning/ending of the string:
where table_name ilike '%PDK%1100090%'
If the double backslash isn't working, then you have some other funkiness in the string.
Replacing the query by adding 3 more backslashes solve the problem. Now it looks like this:
select * from my_table where table_name ilike 'PDK\\\\1100090'

SQL Replace comma in results without using replace

I feel like this should be simple enough to do, but have not found any solutions that didn't use replace so far. I have the following select statement I am running, and for some of the columns there are commas separating the values. I would like to replace these commas with semicolons, however I only want to do it in the select statement. I don't want it to alter the values in the tables at all. This is not a one off statement either, or I'd just replace all the commas with semicolons and then revert back.
SELECT a.Category_Id, a.Category_Name, ISNULL(b.Category_Alias, '') as Category_Alias,
ISNULL(b.SUPPORT_NAMES, '') as SUPPORT_NAMES
FROM Categories a
INNER JOIN CategoryInfo b on b.Category_Id=a.Category_Id
For the Category_Alias column, the records are actually stored like CS, Customer Support and I want that to show up as CS; Customer Support just for the select statement.
I believe you may be confused as to what the REPLACE function is doing. You can use REPLACE within your SELECT statement without altering the data in the database:
SELECT REPLACE(MyField, ',', ';') AS NewFieldName
FROM MyTable
I believe you don't want to replace the value physically in the table, but ok to replace on select
So you can
Select REPLACE(ColumnName,',',';')
From TableName
Most SQL servers implement an inline replace function. Most of them are named replace(), and can also be used in a select statement.
Example from MySQL:
SELECT field, REPLACE(field,',',';') FROM my_table;

How can I search for numbers in a varchar column

I've got a simple nvarchar(25) column in an SQL database table. Most of the time, this field should contain alphanumeric text. However, due to operator error, there are many instances where it contains only a number. Can I do a simple search in SQL to identify these cases? That is, determine which rows in the table contain only digits in this column. As an extension, could I also search for those column values which contain only digits and a space and/or slash.
In other languages (eg. Perl, Java) a regular expression would resolve this quickly and easily. But I haven't been able to find the equivalent in SQL.
yes you can achive this by using isnumeric function available in sql sever
check more at this link : http://msdn.microsoft.com/en-us/library/aa933213(SQL.80).aspx
All the answers referred to the isnumeric function, but they are not correct, I've mentioned in a comment for all the answers the flaw in the answers. The correct solution is to use a regular expression in your where clause, which contains not like '%[^0-9]%', see the example below:
select column_name from table_name where column_name not like '%[^0-9]%'
select column_name from table_name where IsNumeric(column_name) <> 1
Numeric Only:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1
With Space:
SELECT * FROM Table WHERE Field LIKE '% %'
With Slash:
SELECT * FROM Table WHERE Field LIKE '%/%'
Combined:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1 OR Field LIKE '% %' OR Field LIKE '%/%'