Link to home
Start Free TrialLog in
Avatar of brokeMyLegBiking
brokeMyLegBiking

asked on

Prevent char() datatype field from padding right of field with spaces.

char() datatype is better because it is faster and it takes up less diskspace than varchar(). However, one problem with char() is that it pads it's contents with spaces on the right. This is a paid when creating reports from these fields because I have to use Trim() statements everywhere. Is there a way to have SqlServer automatically trim the spaces to the right when extracting the value of a char() field?
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

>char() datatype is better because it is faster and it takes up less diskspace than varchar().

this is no longer true as by today. only use a char only when you have short columns or really fixed-lenght columns
instead of trim(x) you could also use cast(x as varchar(300))   :=)
U can use Trim or use CAST(urColumn as varchar(255))
Avatar of brokeMyLegBiking
brokeMyLegBiking

ASKER

Angel, what do you mean that varchar() doesn't take up any more space? Is that as of version 2005?

If that is the case, then that is good news. So indexes also don't take up any more space with varchar?

(I am using SqlServer version 2005)
You can use RTrim to remove spaces on the right

http://msdn2.microsoft.com/en-us/library/ms178660(SQL.90).aspx
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial