using trim in a select statement - sql

I have a table, my_table, that has a field my_field. myfield is defined as VARCHAR(7). When I do:
SELECT myfield
FROM my_table;
I get what appears to be the entire 7 characters, but I only want the actual data.
I tried:
SELECT TRIM(myfield)
FROM my_table;
and several variations. But instead of getting 'abcd', I get 'abcd '.
How do I get rid of the trailing blanks?

As others have said:
trim whitespace before data enters the database ("Mop the floor...);
ensure this is not actually a column of type CHAR(7).
Additionally, add a CHECK constraint to ensure no trailing spaces ("...fix the leak.") While you are at it, also prevent leading spaces, double spaces and zero-length string e.g.
CREATE TABLE my_table
(
myfield VARCHAR(7) NOT NULL
CONSTRAINT myfield__whitespace
CHECK (
NOT (
myfield = ''
OR myfield LIKE ' %'
OR myfield LIKE '% '
OR myfield LIKE '% %'
)
)
);-

VARCHAR columns will not pad the string you insert, meaning if you are getting 'ABCD ', that's what you stored in the database. Trim your data before inserting it.
Make sure you are not using the CHAR datatype, which will pad your data in the way you suggest. In any case:
SELECT TRIM(myfield) FROM mytable;
will work.
Make sure also that you are not confusing the way the SQL interpreter adds padding chars to format the data as a table with the actual response.

Make sure that you are not inserting data in this column from a CHAR(7) field.

You need to trim your result when selecting as opposed to when inserting, eg:
SELECT TRIM(myfield) FROM my_table;

Related

Not able to TRIM trailing space in varchar column in oracle

I am using oracle 11g DB and in one of the table having varchar column has data which has trailing spaces. I have tried TRIM function to update the column but still the space at the end of the string prevails. What can be the reason for the trailing space? and how to fix this issue.
Column contents are displayed as below.
select '<'||mycol||'>' from mytab
Output : <mysamplestring >
select DUMP(mycol) from mytab
Output : Typ=1 Len=28: 67,114,117,100,101,32,80,101,116,114,111,108,101,117,109,32,69,120,116,114,97,99,116,105,111,110,194,160
Thanks
Using UNISTR did the trick for me. It removed the characters 160 and 194
update mytab set mycol = REPLACE(mycol,unistr('\00A0'),'')
trim() should work as expected! Sure it is a varchar2 column not a char column?
update hr.countries t
set t.country_name = t.country_name || ' ';
update hr.countries t
set t.country_name = trim(t.country_name);
Change the column type from char to varchar2, char column blank-pads the value with spaces to fill up the size of the field. So for a char(10) column, when you insert 'abc', it will automatically be inserted as 'abc '

SQL REPLACE not working as expected

I have a temp table that I'm trying to eliminate all the white spaces from a specific column. However my replace isn't working at all. Here's the code I have
IF OBJECT_ID('tempdb..#attempt1temptable') IS NOT NULL
BEGIN
DROP TABLE #attempt1temptable
END
GO
CREATE TABLE #attempt1temptable
(
temp_description varchar(MAX),
temp_definition varchar(MAX)
)
INSERT INTO #attempt1temptable
SELECT graphic_description, graphic_definition
FROM graphic
UPDATE #attempt1temptable SET temp_description=REPLACE(temp_description, ' ', '')
UPDATE #attempt1temptable SET temp_description=REPLACE(temp_description, char(160), '')
--I have no idea why it won't update correctly here
select temp_description, LEN(temp_description) from #attempt1temptable
The Insert and select work as expected however it's not updating temp_description to have no white spaces. The result of the query gives me the temp_description without anything changed to it. What am I doing wrong here?
Try replacing some other whitespace characters:
select replace(replace(replace(replace(
description
,char(9)/*tab*/,'')
,char(10)/*newline*/,'')
,char(13)/*carriage return*/,'')
,char(32)/*space*/,'')
from #attemp1temptable
You are probably dealing with other characters than space. You could be dealing with tab for example.
I would suggest to copy and paste the character to remove from the actual data into your replace statement to ensure you have the right character(s).
Edit :
Also, you seem to use LEN to verify if the data was updated or not. However, keep in mind that LEN doesn't count trailing white space as character. So the count might not change even if the data was updated

How to edit fields by choosing what to be edited and leaving the rest untouched

I have to edit a all fields of a column. So far it is easy :D , but the problem is that the fields contains text and numbers. And I do need to replace the text only and leave the numbers untouched. What is more the text is in unicode which is making the task even harder lol. I tried to use this query but without success ..
UPDATE table_name SET field = REPLACE(field, ' ', 'My text')
If you are using Oracle/Postgresql you can use REGEX_REPLACE to replace non-digit:
UPDATE table_name
SET field = regexp_replace( field, '[^[:digit:]]', null ) ;
SqlFiddleDemo

SQL table trailing space cause errors

I had an error in my C# code and after debugging found that
this is because on entry in a table has a trailing space.
For example 'aaa '
Now in my C# code try to set a Selected values of a combo box to this(combobox as an item with value of 'aaa' and this fails.
Clearly The solution is to fix the DB 'aaa '
Now I go to DB
and do this :
Select * from mytable
where Name='aaa'
I get Name 'aaa'
Select * from mytable
where Name='aaa '
Again I get Name 'aaa'
Select len(name) from My table
where name ='aaa'
I get 3
My question is: how by querying the table I know it has an extra space for 'aaa '?
SQL server len function excludes trailing blanks.
Consider using the DATALENGTH (Transact-SQL) function which does not trim the string.
So you can check if datalength(column) <> datalength(rtrim(column)) to find if it contains trailing spaces.
Note: if processing a unicode string, DATALENGTH will return twice the number of characters.
As Giorgi Nakeuri has explained, 'a ' and 'a' are considered equal. But you can easily find trailing spaces with LIKE:
select * from mytable where name like '% ';
And here is how to update:
update mytable set name = rtrim(name) where name like '% ';

Update oracle table and change one character with another

I have this table where one of the Fields contains values like this ¤1¤. It is in an Unicode database and nvarchar2 Fields.
I then want to switch the ¤ With an ? and Write this line:
update table1 set col1 = REPLACE(col1,'¤','?');
commit;
The col1 is not updated.
What am I doing wrong?
select ascii('¤') from dual;
Even though this states 164 as the result on a non-unicode database, it doesn't so on a unicode database. Hence the replace as asked for, will not work.
select chr(164 USING NCHAR_CS) from dual;
Using this states '¤' as the result.
Hence the following replace should work.
select replace(col1, chr(164 USING NCHAR_CS), chr(63)) from table1;
Sometime simple cut and paste may not get the character properly in your query so .. instead of writing the character in your query.. get the ascii value (you can use ascii or dump function in oracle to get the ascii value of the character) of the character and then use that in your replace as below
Your character seems to me ASCII value 164 ...
-- try as below
update table1 set col1 = replace(col1,chr(164),'?');
commit;