db2 concatenated rows displaying whitespace - sql

I have a column in a DB2 table where the rows of data consist of two strings, string1 and string2. These have a number of whitespace characters in between them, which I'm trying to remove.
When I run the following, I see the expected result, which is string1 string2.
SELECT REPLACE(COLUMN,' ','') FROM Source_Table;
However when I try to insert the cleansed rows into a new table, the data still contains whitespace between string1 and string2.
INSERT INTO Target_Table (Column)
SELECT REPLACE(Column,' ','') FROM Source_Table;

You probably declared your column as CHAR and DB2 is filling unused space in your column with spaces. That's by design.
If you want to avoid this behavior you should declare the column as VARCHAR
Also in your SELECT you should use the TRIM or RTRIM function for removing the withespaces instead of REPLACE()

You can use trim / rtrim like above said you, or strip function

Related

right() function not working without using rtrim. only working like this :right(rtrim(column_name),value) WHY?

I don't understand why the right() function won't work without rtrim.
I even used update to get rtrim for entire column but still right() wont work without using Rtrim() inside it.
I made a table in SQL Server 2008. The table has only one column name char(30).
I tried to get a substring counting from right side using the right() function.
The result shows just blank column.
When I try using left() function to get substring from left side it works as expected but right() function doesn't work without using RTRIM.
THIS WILL WORK
select right(rtrim(name),5) from emp
THIS WONT WORK
select right(name,5) from emp
I expect to get substring of last 3 characters in name column.
My result shows an empty column
char(5) is a fixed-length type so the value includes trailing spaces in cases where the value is less than the specified max size. When you use RIGHT, the trailing spaces are returned:
DECLARE #name char(30) = 'abcdef';
SELECT RIGHT(#name,5); --returns 5 spaces
Using TRIM, the trailing spaces are removed and the returned data type is varchar(5):
SELECT RIGHT(RTRIM(#name),5); --returns 'bcdef'
You should probably use varchar(30) instead of char(30) for a Name column because the data will vary and length and you probably don't want to store the unneeded spaces in the database. char is most appropriate when the actual values are the same size.

Remove multiple values from a field with comma delimited values - SQL

I have to make some changes to a specific field (Oracle DB)
I would like to know what is the best way to remove multiple values from a field with comma delimited values (string) ?
Example:
Before: TYP,CRT,REW,PBR,ORT
Remove TYP, CRT and ORT
After: REW,PBR
Is using a nested REPLACE the only option ?
You can use REGEXP_REPLACE:
UPDATE myTable
SET myColumn =
TRIM(TRAILING ',' FROM REGEXP_REPLACE(myColumn, '(TYP|CRT|ORT)(,|$)'))
The regex looks for TYP, CRT, or ORT followed by a comma or the end of the string. If it gets the very last value (for example the ORT in REW,ORT) it will leave a trailing comma. Rather than overcomplicate the regex, this example removes any trailing commas using the TRIM() function.
There's a SQLFiddle here.
Finally, Frank Schmitt's comment above is spot on - a comma-separated list like this in a column often means poor design. If you can split these values into a related details table you'll probably make things a lot easier.

DBFirst, EF, MVC - inserting spaces in fields

My dbfirst EF with MVC project is inserting values into the SQl database with spaces at the end of the value. In addition to trimming the spaces in my sql queries, is there anything I can do right in the code itself to insert values without spaces into the SQL table?
It could just be that you have a char column instead of a varchar. http://msdn.microsoft.com/en-us/library/aa258242(v=sql.80).aspx
[n]Char columns are fixed length, so even if you only have a 3 char string, it will add spaces to fill up the whole column. You'll want [n]VarChar if you want a variable length string.
Trim the spaces before you insert.
You can use string.Trim()
http://msdn.microsoft.com/en-us/library/t97s7bs3.aspx

Select number of comma delimited list items

I have a column that has comma seperated values and I need to select all rows that have 13 commas. They seperate numbers so I don't need to worry about any strings that contain commas. How would I do this?
alternative to like (I do not like the like, and the above will fail if contains 14 commas or more)
select * from table
where length(replace(your_column, ',', ''))=length(your_column)-13;
for better utilize the index, you should seek to normalize your table
If you're using PostgreSQL, you could also use regular expressions.
However, a better question might be why you have a single column with comma-separated values instead of multiple columns.
If you count a string with 14 commas as having 13 commas, then this will work:
SELECT * FROM table WHERE column LIKE '%,%,%,%,%,%,%,%,%,%,%,%,%,%'
% matches any string (including zero length).
In PostgreSQL you can do:
select col from table where length(regexp_replace(col, '[^,]', '', 'g')) = 13;

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 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;