Not able to TRIM trailing space in varchar column in oracle - sql

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 '

Related

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;

Alter All Column Values using TRIM in SQL

I want to modify (Remove all white spaces) all the values of Column
"Name" .
How do I use TRIM and modify all column values of Name .
Example :
Before : " rishi.ranka " After : "rishi.ranka"
Thank you very much in Advance.
Use a combination of LTRIM (Left Trim) & RTRIM (Right Trim):
UPDATE [TableName]
SET [Name]=LTRIM(RTRIM([Name]))
If the datatype of name column is varchar then don't need to use rtrim function the right side spaces will be automatically trim. use only LTRIM only.
update tablename
set name = ltrim(name)
where <condition>;
Run this see the how it trims the right spaces automatically.
DECLARE #mytb table
(
name varchar(20)
);
INSERT INTO #mytb VALUES (' stackoverflow ');
SELECT len(name) from #mytb;
SELECT ltrim(name),len(ltrim(name)) from #mytb;

using trim in a select statement

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;

Insert a trailing space into a SQL Server VARCHAR column

I'm trying to insert trailing spaces into a VARCHAR(50) column and the SQL insert seems to be cutting them off. Here's my code:
create table #temp (field varchar(10));
insert into #temp select ' ';
select LEN(field) from #temp;
Unfortunately, this returns a length of zero, meaning the ' ' was inserted as a ''. I need a blank space to be inserted for this column - any ideas?
Use DATALENGTH, not LEN, because LEN doesn't process spaces.
Take this zero length string, for example:
SELECT LEN(' ') AS len,
DATALENGTH(' ') AS datalength
Results:
len datalength
-----------------
0 1
From http://msdn.microsoft.com/en-us/library/ms190329.aspx:
LEN Returns the number of characters of the specified string expression, excluding trailing blanks.
You better be aware also that SQL Server follows ANSI/ISO SQL-92 padding the character strings used in comparisons so that their lengths match before comparing them. So, you may want to use LIKE predicate for comparisons [1]
[1]
How SQL Server Compares Strings with Trailing Spaces
http://support.microsoft.com/kb/316626

What is the easiest way using T-SQL / MS-SQL to append a string to existing table cells?

I have a table with a 'filename' column.
I recently performed an insert into this column but in my haste forgot to append the file extension to all the filenames entered. Fortunately they are all '.jpg' images.
How can I easily update the 'filename' column of these inserted fields (assuming I can select the recent rows based on known id values) to include the '.jpg' extension?
The solution is:
UPDATE tablename SET [filename] = RTRIM([filename]) + '.jpg' WHERE id > 50
RTRIM is required because otherwise the [filename] column in its entirety will be selected for the string concatenation i.e. if it is a varchar(20) column and filename is only 10 letters long then it will still select those 10 letters and then 10 spaces. This will in turn result in an error as you try to fit 20 + 3 characters into a 20 character long field.
MattMitchell's answer is correct if the column is a CHAR(20), but is not true if it was a VARCHAR(20) and the spaces hadn't been explicitly entered.
If you do try it on a CHAR field without the RTRIM function you will get a "String or binary data would be truncated" error.
Nice easy one I think.
update MyTable
set filename = filename + '.jpg'
where ...
Edit: Ooh +1 to #MattMitchell's answer for the rtrim suggestion.
If the original data came from a char column or variable (before being inserted into this table), then the original data had the spaces appended before becoming a varchar.
DECLARE #Name char(10), #Name2 varchar(10)
SELECT
#Name = 'Bob',
#Name2 = 'Bob'
SELECT
CASE WHEN #Name2 = #Name THEN 1 ELSE 0 END as Equal,
CASE WHEN #Name2 like #Name THEN 1 ELSE 0 END as Similiar
Life Lesson : never use char.
I wanted to adjust David B's "Life Lesson". I think it should be "never use char for variable length string values" -> There are valid uses for the char data type, just not as many as some people think :)
The answer to the mystery of the trailing spaces can be found in the ANSI_PADDING
For more information visit: SET ANSI_PADDING (Transact-SQL)
The default is ANSI_PADDIN ON. This will affect the column only when it is created but not to existing columns.
Before you run the update query, verify your data. It could have been compromised.
Run the following query to find compromised rows:
SELECT *
FROM tablename
WHERE LEN(RTRIM([filename])) > 46
-- The column size varchar(50) minus 4 chars
-- for the needed file extension '.jpg' is 46.
These rows either have lost some characters or there is not enough space for adding the file extension.