INF: Behavior of ANSI_PADDING
This article was previously published under Q154886
This article has been archived. It is offered "as is" and will no longer be updated.
This article discusses the behavior of the SET ANSI_PADDING optionavailable with SQL Server version 6.5 and later.
One of the new SET options introduced with SQL Server 6.5 is ANSI_PADDING.The "What's New in SQL Server 6.5" guide mentions that when ANSI_PADDING ison, Varchar values are padded with blanks and Varbinary values arepadded with nulls. Note that this does not mean that all variable columnsstart behaving like fixed length columns by padding all values entered intothe column. It means that if a value is entered in a variable column withtrailing blanks or nulls, the trailing blanks or nulls are notautomatically removed.
The running of the following script in ISQL/w or Query Analyzer illustrates the behavior of ANSI_PADDING. It builds a table with Char, Varchar and Varbinary columns and inserts values both with and without trailing blanks. The script does this twice, once with ANSI_PADDING on and once with it off, to demonstrate that the trailing blanks are inserted into all columns for the first row when the option is on, and are not inserted when the option is off. It also illustrates that the option does not cause the Varchar columns to be padded out to their full length. It only prevents the truncation of trailing blanks supplied by the user.
USE pubsGOSET ANSI_PADDING ONGOPRINT 'Testing with ANSI_PADDING ON'GOCREATE TABLE t1 (charcol char(16) NULL, varcharcol varchar(16) NULL, varbinarycol varbinary(8))GOINSERT INTO t1 VALUES ('No blanks', 'No blanks', 0x00ee)INSERT INTO t1 VALUES ('Trailing blank ', 'Trailing blank ', 0x00ee00)GO SELECT 'CHAR'='>' + charcol + '<', 'VARCHAR'='>' + varcharcol + '<',varbinarycolFROM t1GOSET ANSI_PADDING OFFGOPRINT 'Testing with ANSI_PADDING OFF'GOCREATE TABLE t2 (charcol char(16) NULL, varcharcol varchar(16) NULL, varbinarycol varbinary(8))GOINSERT INTO t2 VALUES ('No blanks', 'No blanks', 0x00ee)INSERT INTO t2 VALUES ('Trailing blank ', 'Trailing blank ', 0x00ee00)GOSELECT 'CHAR'='>' + charcol + '<', 'VARCHAR'='>' + varcharcol + '<', varbinarycolFROM t2GODROP TABLE t1GODROP TABLE t2GO
Article ID: 154886 - Last Review: 12/04/2015 15:14:06 - Revision: 3.3
Microsoft SQL Server 6.5 Standard Edition, Microsoft SQL Server 7.0 Standard Edition, Microsoft SQL Server 2000 Standard Edition
- kbnosurvey kbarchive kbinfo kbnetwork KB154886