Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I believe the answer to this question is "no" but I'm interested in the community opinion. A varchar or nvarchar value should automatically trim trailing whitespace, so I don't believe I should ever have to call RTRIM() on such a value. Does any expert have a reason that I would need to?

(In case the tags do not make it clear, I'm referring specifically to Microsoft SQL Server.)

like image 956
John Bledsoe Avatar asked Nov 01 '10 19:11

John Bledsoe


2 Answers

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

like image 141
heisenberg Avatar answered Oct 08 '22 00:10

heisenberg


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 #temp
values ('test   ')
insert #temp
values ('test2 ')
insert #temp
values ('test    ')
insert #temp
values ('test')

select test + '1' from #temp
select rtrim(test) +'1' from #temp
select * from #temp where test = 'test'
like image 45
HLGEM Avatar answered Oct 08 '22 01:10

HLGEM