Given the SQL statements below, I would expect the result of the query to be:
|testX|5|
|XXXXX|5|
This is because the column is a char(5) and I would expect it to insert blank trailing spaces to fill the remaining space in the column since the inserted values are less than five characters. However, the query actually produces the following result:
|test|4|
|X|0|
Can someone please explain this. Why aren't the values padded with trailing blanks to make them fill up the length 5 column? Why does the REPLACE function insert a single X for the second value when it is a completely empty string?
USE Test;
GO
SET ANSI_PADDING ON;
GO
CREATE TABLE BlankTest
(
Value char(5) NOT NULL
);
GO
INSERT INTO BlankTest (
Value
) VALUES (
'test'
);
GO
INSERT INTO BlankTest (
Value
) VALUES (
''
);
GO
SELECT REPLACE(Value, ' ', 'X'), LEN(Value)
FROM BlankTest;
GO
DROP TABLE BlankTest;