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?
LVL 4
brokeMyLegBikingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
instead of trim(x) you could also use cast(x as varchar(300))   :=)
0
Aneesh RetnakaranDatabase AdministratorCommented:
U can use Trim or use CAST(urColumn as varchar(255))
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

brokeMyLegBikingAuthor Commented:
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)
0
SireesCommented:
You can use RTrim to remove spaces on the right

http://msdn2.microsoft.com/en-us/library/ms178660(SQL.90).aspx
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>char() datatype is better because it is faster and it takes up less diskspace than varchar().
that sentence is acutally wrong, CHAR never uses less diskspace than varchar!

let me clarify:

CHAR(50) will occupy 50 bytes for every row you store. That was always so and is still a fact (not only for SQL Server)
VARCHAR(50) will occupy only that much space per row as you give as non-trailing spaces, That is so since at least SQL Server 6.5.

The main reason why both data types existed is that in earlier times, when CPU power was expensive while VARCHAR as varying-sized field required quite a lot of additional CPU power over the CHAR data type, the choice was much more crutial.
Today, you will not really see the difference between CHAR() and VARCHAR(), but more about disk space usage, which is still the slowest part in database operations.

Hence, you will "ALWAYS" use VARCHAR (resp. NVARCHAR) for the string fields, unless you have specify reasons to use CHAR().
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.