When I am saving data into a table, extra spaces being added to the valued at the tail. I observed that as the column length is 5, if I am inserting a value of 3 char length, 2 extra spaces are being added. Can any one how to solve this problem.
SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.
To concatenate two string type columns separated by space, we can use space function. Notice the SPACE(1) function in between FirstName and LastName. As the parameter value passed in SPACE function is 1 so there will be one blank space in between FirstName and LastName column values.
To select a column name with spaces, use the back tick symbol with column name. The symbol is ( ` `). Back tick is displayed in the keyboard below the tilde operator ( ~).
Column names can contain any valid characters (for example, spaces).
Is the column type CHAR(5)
instead of VARCHAR(5)
?
CHAR(x)
creates a column that always stores x characters, and pads the data with spaces.VARCHAR(x)
creates a column that varies the lengths of the strings to match the data inserted.This is a property of CHAR
data type. If you want no extra spaces, you need to use VARCHAR
although for a small field there is a minimal overhead compared to standard CHAR
. Having said that, it is believed that VARCHAR
nowadays is as good as CHAR
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With