Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 char vs varchar + padding issue

Author  Topic 

petebob796
Starting Member

35 Posts

Posted - 2008-02-20 : 17:13:33
I have upsized some tables from ms access (using the wizard) which has created many nvarchar fields. I know the system doesn't need unicode characters so I straight away changed them to varchar and the system is all working but am now deciding on which ones to change to char as I understand you get a performance gain. I am trying to do this without changing much ms access front end code so don't want to deal with rtrim I am thinking change the ones I can guarantee the length of. These happen to be the primary keys.

My questions are is this the correct way of doing things, am I correct in assuming I would have to rtrim alot in program code if I changed all to char.

Finally and. most importantly what about a single character field which could be null can I make this a char(1) and it will still return null if empty as it would as a varchar(1). I suppose this question is how does the padding work is it the character followed by a number of nulls (ascii 0 I think).

Sorry for the multiple questions but I think they are all related

sw0rdf1sh7
Starting Member

15 Posts

Posted - 2008-02-21 : 10:29:56
I'll reply to one of your questions.

Fixed length use char
if varying in length use varchar

You probably know this already. =)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-02-21 : 10:49:51
Remember: SQL does not use trailing spaces when comparing values. So, a CHAR(4) of 'X---' (where - is a space) is considered equal to a VARCHAR(4) of 'X'. This is for joins, comparisons, etc. You should only need to use RTRIM() on a CHAR() if you are concatenating or using pattern matching or doing string manipulation or things like that.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -