Home >> FAQs/Tutorials >> SQL Server FAQ

SQL Server FAQ - CHAR(n) - Truncating and Padding Fixed Length Strings

By: FYIcenter.com

(Continued from previous topic...)

How Fixed Length Strings Are Truncated and Padded?

When the length of the input string does not match the storage size of the fixed length string data type CHAR(n). SQL Server will:

  • If the input string of CHAR(n) has less than n bytes, it will be padded with space characters to become n bytes.
  • If the input string of CHAR(n) has more than n bytes, it will be truncated to n bytes.

The tutorial exercise shows you good examples of truncating and padding fixed length character strings:

 
-- Length matches the data type size
DECLARE @msg CHAR(36);
SET @msg = 'Welcome to FYIcenter.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to FYIcenter.com SQL Server!)

-- Length is bigger than the data type size - truncated
DECLARE @msg CHAR(24);
SET @msg = 'Welcome to FYIcenter.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to FYIcenter.com)

-- Length is smaller than the data type size - padded
DECLARE @msg CHAR(46);
SET @msg = 'Welcome to FYIcenter.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to FYIcenter.com SQL Server!          )

(Continued on next topic...)

  1. What Is a Constant or Literal?
  2. How To Write Character String Constants or Literals?
  3. What Is a Collation?
  4. How To Specify the Collation for a Character Data Type?
  5. What Happens If Strings Are Casted into Wrong Code Pages?
  6. How To Find Out What Is the Default Collation in a Database?
  7. How Fixed Length Strings Are Truncated and Padded?
  8. How To Enter Unicode Character String Literals?
  9. How To Enter Binary String Literals?
  10. How To Enter Date and Time Literals?
  11. Why I Can Not Enter 0.001 Second in Date and Time Literals?
  12. What Happens If Date-Only Values Are Provided as Date and Time Literals?
  13. What Happens If Time-Only Values Are Provided as Date and Time Literals?
  14. What Are Out-of-Range Errors with Date and Time Literals?
  15. What Happens If an Integer Is Too Big for INT Date Type?
  16. How Extra Digits Are Handled with NUMERIC Data Type Literals?
  17. How REAL and FLOAT Literal Values Are Rounded?
  18. What Are the Underflow and Overflow Behaviors on FLOAT Literals?

Related Articles:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...