Trim spaces in a T-SQL INSERT

Posted on 2013-09-23
Medium Priority
Last Modified: 2013-10-03
I'm running an insert T-SQL statement to inter one record from one table to another. There is an nvarchar(100) field in the insert. Even if a wrap the nvarchar field in RTRIM or SUBSTRING, the insert result is that the target table field includes the padding of spaces beyond the value of the field. For example, if the value of the source field is "Answer", the value in the target field after the insert is "Answer                                                                                            ", i.e., the value + spaces. Any thoughts?
Question by:paulmcneil

Accepted Solution

Beartlaoi earned 2000 total points
ID: 39516012
It sounds like the target column is defined as a fixed width string rather than variable.
Verify the type of the target column is an nVARchar or other variable string.
LVL 49

Expert Comment

ID: 39516985
& This should tell you what that field's data type is. No points pl.
    , t.name as type_name
    , c.max_length
    , c.collation_name
    , c.column_id
    , c.is_nullable
FROM sys.columns AS c
INNER JOIN sys.types AS t ON c.system_type_id = t.system_type_id

WHERE object_id = OBJECT_ID('dbo.YourTableName') --<< change to suit

AND c.name = 'TheFieldName' --<< change to suit

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question