I was checking if DB pads spaces in a column if the inserted string has fewer characters than the designated length of the column. Example:
lets say size of <column1>
is 10 but the value entered is abc – then is it abc_______
which the DB stores where _
represents spaces?
I am asking because I used LTRIM-RTRIM
while INSERTing the values and on again fetching the value in the very next minute I got the result as abc_______
.
Advertisement
Answer
You are using the CHAR
or CHARACTER
datatype for the column. The CHAR
or CHARACTER
datatype is a fixed length datatype and is padded with space at the end of the value to fill the column size.
You can use VARCHAR
to avoid the padding with spaces at the end of the values.
Note: Make sure you are using CHARACTER_LENGTH
on CHARACTER
columns to get the correct character length (without padding spaces). The result of LENGTH
also includes the padding spaces.