Should I ever need to call RTRIM() on a varchar or nvarchar value? Should I ever need to call RTRIM() on a varchar or nvarchar value? sql sql

Should I ever need to call RTRIM() on a varchar or nvarchar value?


If ANSI_PADDING is ON then trailing spaces will be stored even with varchar/nvarchar data types, so yes.


You may not need to rtrim to get the values out in a simple select, but if you want to concatentate the values (such as combining first and last names to show the full name) you may need to.

Run this test to see what I mean:

create table #temp (test varchar (10))insert #tempvalues ('test   ')insert #tempvalues ('test2 ')insert #tempvalues ('test    ')insert #tempvalues ('test')select test + '1' from #tempselect rtrim(test) +'1' from #tempselect * from #temp where test = 'test'


In theory, yes, because of SET ANSI_PADDING which is ON by default and will ON always in future.

To be honest, I tend to RTRIM on write because of this to avoid having on read which happens far more often. It only has to happen once to spoil your day...