I was about to write in that the behaviour around trailing spaces may not be the same always, but Mitesh & Vadivel have already mentioned it.
Also, please note that the behaviour around storage of trailing spaces also differs based on the setting of ANSI_PADDING (ON by default). Here's a simple test:
USE tempdb
GO
--Change the ANSI_PADDING setting
SET ANSI_PADDING OFF
GO
--Create a test table
CREATE TABLE TrailingSpacesCheck (RowId INT IDENTITY(1,1),
RowValue VARCHAR(20)
)
GO
--Insert some test data
INSERT INTO TrailingSpacesCheck (RowValue) VALUES (' Nakul'),
('Nakul '),
('Nakul')
GO
--Select the data
SELECT RowId, RowValue, LEN(RowValue) AS NumberOfCharacters, DATALENGTH(RowValue) AS NumberOfBytesUsed
FROM TrailingSpacesCheck
GO
/*
---------------------------
RESULTS - ANSI_PADDING OFF
---------------------------
RowId RowValue NumberOfCharacters NumberOfBytesUsed
1 Nakul 7 7
2 Nakul 5 5
3 Nakul 5 5
------------------------------------
RESULTS - ANSI_PADDING ON (Default)
------------------------------------
RowId RowValue NumberOfCharacters NumberOfBytesUsed
1 Nakul 7 7
2 Nakul 5 7
3 Nakul 5 5
*/
--Restore the ANSI_PADDING setting
SET ANSI_PADDING ON
GO
--Cleanup
IF OBJECT_ID('TrailingSpacesCheck') IS NOT NULL
BEGIN
DROP TABLE TrailingSpacesCheck
END
GO
commented on Jul 14 2012 2:20PM