How Fixed Length Strings Are Truncated and Padded?
Submitted by: AdministratorWhen 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 GlobalGuideLine.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to GlobalGuideLine.com SQL Server!)
-- Length is bigger than the data type size - truncated
DECLARE @msg CHAR(24);
SET @msg = 'Welcome to GlobalGuideLine.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to GlobalGuideLine.com)
-- Length is smaller than the data type size - padded
DECLARE @msg CHAR(46);
SET @msg = 'Welcome to GlobalGuideLine.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to GlobalGuideLine.com SQL Server!)
Submitted by: Administrator
* 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 GlobalGuideLine.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to GlobalGuideLine.com SQL Server!)
-- Length is bigger than the data type size - truncated
DECLARE @msg CHAR(24);
SET @msg = 'Welcome to GlobalGuideLine.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to GlobalGuideLine.com)
-- Length is smaller than the data type size - padded
DECLARE @msg CHAR(46);
SET @msg = 'Welcome to GlobalGuideLine.com SQL Server!';
PRINT '('+@msg+')';
GO
(Welcome to GlobalGuideLine.com SQL Server!)
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | What Happens If NULL Values Are Involved in Bitwise Operations? |
☺ | PHP MSSQL - How To Display a Past Time in Days, Hours and Minutes? |
☺ | How to download Microsoft SQL Server 2005 Express Edition? |
☺ | Can You Roll Back the DDL Statement in a Trigger? |
☺ | What Is a Subquery in a SELECT Query Statement in MS SQL Server? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Sybase Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |