Why I prefer nvarchar, nchar and ntext over varchar, char and text fieldtypes

When designing SQL-server databases I like to use fields starting with an "N".

The "N" stands for NATIONAL, which overhere in Europe we have a lot of, umlauts, trema's, accents... you name it, we love it!

We use a lot of unicode data, things that VARCHAR has difficulties handling, but that's not the main reason I like to use the "N" fields.

The reason itself is that fields without the "N" are padded with spaces to meet their maximum when you output them. This causes a lot of problems when trying to create relations or output them to fields.

For example, an NVARCHAR at length 20 with content "BH 90210" outputs this way:

<input type="text" name="Postcode" value="BH 90210">

where VARCHAR at length 20

<input type="text" name="Postcode" value="BH 90210
"
>

Where the value is padded with 12 more spaces to be used with VARCHAR...

Although we in Europe are aware of this when designing SQL databases, I still get a lot of SQL-databases that use the VARCHAR, TEXT or CHAR fields which are a nightmare to use in CF, because of the way you creating lookups and relations sometimes. I have "TRIM()" on speed dial here to create good content.

Another SQL trick I use quite a lot, which does solve most problems is:

SELECT rtrim(ID) as ID, rtrim(Pricelist) as Pricelist

It's good design to not use the "N", since an NCHAR takes twice the byte-space of a CHAR, but considering the above, please use "N" when creating SQL databases.

Comments
Michael's Gravatar While I agree that nchar and nvarchar are what you should use for unicode (also pages should be coded as UTF-8 for mulit-language support) I think you misunderstand varchar and char. varchar does not pad the contents, char will fill the filed with spaces; this is the same for nvarchar and nchar. I would guess the your entry has an chr(10) or char(13) (line feed and carriage return) This sometimes gets added when you copy and paste from Word, in my experience.
# Posted By Michael | 9/21/07 7:30 PM
Pablo Vos's Gravatar My entry was deliberately padded by using a break to show how code might look like afterwards.

Whenever I use fixed size varchars en output them using CF I get spaces added to fill the field.
# Posted By Pablo Vos | 9/21/07 7:47 PM

BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.