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
Related
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.
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
I am importing data into redshift using the SQL COPY statement. The data has comma thousands separators in the numeric fields which the COPY statement rejects.
The COPY statement has a number of options to specify field separators, date and time formats and NULL values. However I do not see anything to specify number formatting.
Do I need to preprocess the data before loading or is there a way to get redshift to parse the numbers corerctly?
Import the columns as TEXT data type in a temporary table
Insert the temporary table to your target table. Have your SELECT statement for the INSERT replace commas with empty strings, and cast the values to the correct numeric type.
On a SQLite database something I thought was very simple doesn't work at least under my conditions.
I have one column with names and some name contains apostrophe ('), which I want to remove. I know all names which contains an apostrophe, so I am not trying to query for apostrophes. I am doing something much simpler:
UPDATE table SET column_name="name surname1 surname2" WHERE column_name="name surname1'surname2";
which doesn't return what I expect. It doesn't produce an error but it doesn't modify any record.
SQL doesn't like reflexivity?
There should be no issue with querying the current value of a column to update the same column. Try escaping the single-quote by doubling it e.g. ''.
See: http://www.sqlite.org/lang_expr.html which reads:
A string constant is formed by enclosing the string in single quotes
('). A single quote within the string can be encoded by putting two
single quotes in a row - as in Pascal.
Therefore, your update should be:
UPDATE table SET column_name='name surname1 surname2' WHERE column_name='name surname1''surname2'
Update: Added explanation of escape mechanism.
How can I join xla_ae_lines.description with ap_invoice_distributions_all.description?
Every time I used this syntax
xla_ae_lines.description = ap_invoice_distributions_all.description
I got no rows selected. They are both in varchar2 datatypes in Oracle 11g. So, what would be the right syntax for this?
Reposting from my comment above:
Trim away trailing and leading blanks from both varchars before comparing them:
trim(xla_ae_lines.description) = trim(ap_invoice_distributions_all.description)
Even better, if leading/trailing blank characters are not expected, consider trimming them when inserting the data to your tables.