Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does my fields have white space after the data? 1

Status
Not open for further replies.

mrgulic

Technical User
Joined
Sep 18, 2001
Messages
248
Location
US
I am using SQL 2005.
Microsoft SQL Server Management Studio 9.00.1399.00

I just started noticing that the data in my fields had trailing white space in it equaling the fieds size less the data.

Example:
"SAN/NAS "

The field type/size is: nchar(25)

Even when I do the following the resulting data still has the while space:

Code:
UPDATE [cmdb].[dbo].[device_type]
   SET [device_type_name] = 'SAN/NAS'
 WHERE [device_type_id] = '4'

I am new to programming and very new to sql. I noticed this while binding the table to a listbox control.
Code:
ddl_deviceType.DataSource = dbu.GetSqlDataReader(sqlDatasource, sqlCatalog, sqlSecurity, sqlSelect);
ddl_deviceType.DataValueField = "device_type_id";
ddl_deviceType.DataTextField = "device_type_name";
ddl_deviceType.DataBind();
ddl_deviceType.Items.Insert(0, new ListItem("- Select Device Type -"));

thanks in advance for your insight.

 
Char and nchar are both padded with spaces. This is by design.

If you want to permanently remove the spaces, then you should change the data type to nvarchar.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
that didnt work, but it did give me the information (padding) to do an informed search on MSDN and i found this.

SET ANSI_PADDING is always ON for nchar and nvarchar. SET ANSI_PADDING OFF does not apply to the nchar or nvarchar data types.
 
Once you convert to nvarchar, you have to update the field to trim the spaces.
Code:
update my table 
set myfield = ltrim(rtrim(myfield))
Never use char unless the data contained in it will always be the same length. For instance, StateAbbreviation is a good candidate for char datatype, StateName is not.

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top