• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

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?
0
brokeMyLegBiking
Asked:
brokeMyLegBiking
1 Solution
 
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now