I have a SQL Server 2008 database. This database has a table with a column named "Name" whose data is not cleaned up. For instance, there are values like 'Hello ', where there is unnecessary whitespace. I want to remove that whitespace from all of the values in the "Name" column. My table looks like this:
MyTable
-------
ID (int)
Name varchar(50)
Description nvarchar(128)
How do I trim up the values in the Name column to remove leading and trailing whitespace?
Thank you!
Use LTRIM and RTRIM:
UPDATE dbo.MyTable SET Name = LTRIM(RTRIM(Name));
Related
I am attempting to change a column name but there is an issue because my original column name has a number.
Here is what my table columns looks like
name price small medium large xl 2xl 3xl
When i do the following
ALTER TABLE tableName
RENAME small TO abc;
It executes well.
However when i do the following
ALTER TABLE tableName
RENAME 2xl TO xxl;
I get error saying syntax error at or near "2"
Does this mean i can never change this column's name because it starts with a numeric value?
Any time a column name begins with a non-alpha character, or contains special characters (spaces, etc) or is a keyword like "from," (but don't do that), you have to put the column name in quotes:
alter table tableName rename "2xl" to xxl;
As an aside, it's generally advisable to avoid object names that require double quotes. It's just more work in everything else. It's not wrong precisely speaking, just more work.
Enclose the identifier with the special characters in double quotes.
ALTER TABLE tablename
RENAME "2xl" TO xxl;
select
replace(Street, char(10), '')
from streettable
Have explored the syntax of replace function ...but how does this piece of code work?
The sample code doesn't change datatype. char(10) is line feed and the code just replaces line feeds with empty symbol in street column(not in table, but on the fly to present. Data in table stays untouched).
If you want to change datatype then you should alter the column. Something like:
alter table streettable alter column Street varchar(777) null
I have two tables. Table A is an operational store and table B is the destination table.
Table A DDL:
Column A Varchar(1000)
Table B DDL:
Column B Varchar(250)
So I'm trying to do an insert of truncated column A as so:
Insert into table B (select left(table a.column a, 249)) , but it gives the error
"error: Value too long for character type"
I have also tried substring to try and truncate the text but to no avail. Please note, that there is also Arabic text in Column A - but it hasn't been an issue in Table A.
Any help / suggestions would be much appreciated!
To get around the issue of multi-byte characters, you can cast your field to the desired VarChar size using ::VarChar([some length]). For your example, you would do:
Insert into table B (select table a.column a::VarChar(250))
The problem is that each Arabic symbol taking more than 1 byte because RedShift is Unicode DB. The varchar definition is in bytes. So to be on safe side you can divide everything by 4.
I'm working with PostgreSQL 9.3.
I have a table with a varchar column that I always want to be filled with lowercase strings.
I could use the Postgres lower function before saving my values, but is there instead a way to define this column as a lowercase column?
You can accomplish this with a simple check in the column:
create table lower_field (
field1 varchar check (field1 = lower(field1))
);
I noticed that Firebird creates duplicate columns for a single table, thus incorrect indices are being used in the query which cause query to be slow. Please example below.
I have 2 tables with the same columns and indices, but when checking the table structure, one table shows duplicate columns
Table A : Name VARCHAR(30)
Age INTEGER
BIRTH_DATE TIMESTAMP
Indices : Name, Birth_date (Asc), Birth_date(Desc)
Table B : Name VARCHAR(30)
Age INTEGER
BIRTH_DATE TIMESTAMP
Name VARCHAR(30)
Age INTEGER
BIRTH_DATE TIMESTAMP
Indices : Name, Birth_date (Asc), Birth_date(Desc)
When joining the table with Table C and order by Birth_date, Table A is using the Birth_date index Ordered, but Table B is not.
Please help! what is the cause behind this? Thank you.
I just had a problem where a duplicate column had been allowed to be created. This request
SELECT a.RDB$FIELD_NAME
FROM RDB$RELATION_FIELDS a
WHERE a.RDB$FIELD_NAME like '%COLUMN_NAME%'
was showing two COLUMN_NAME lines. By copy pasting the fields elsewhere it became apparent that one column had trailing whitespace, while the other had a carriage return + line feed (CRLF) and then trailing whitespace.
The FlameRobin wizard was used to create the column. My take on it is a copy-paste was used and a CRLF was inserted. Excel and other softwares can do that to you. FlameRobin, FlameRobin's driver and FireBird should each guard against that, though.
We dropped the offending column by crafting some DDL which had the offending CRLF in the column name.