Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra spaces being added at the tail in the column

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.

like image 933
SKumar Avatar asked Dec 29 '10 17:12

SKumar


People also ask

How do I remove extra spaces in SQL?

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.

How do you add a space when combining a column in SQL?

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.

How do you give column names with spaces?

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 ( ~).

Can column names have spaces?

Column names can contain any valid characters (for example, spaces).


2 Answers

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.
like image 79
Chris Shaffer Avatar answered Oct 09 '22 18:10

Chris Shaffer


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.

like image 44
Aliostad Avatar answered Oct 09 '22 18:10

Aliostad