views:

107

answers:

3

Can somebody provide some best practices when storing special characters such as the trademark (tm or r) or copyright (c)? I am storing them in a varchar field with other text in SQL Server, and displaying on an ASP.NET webpage. Right now we are storing the special character itself and displaying that.

Thanks for any help!

+2  A: 

I am storing them in a varchar field ...

There's one problem, at least. For text that could have "special" characters, you need nvarchar.

Joel Coehoorn
+1 Also it should be noted that it's unless you're expecting to allow "special characters" or "foreign language characters", you'll probably want to stick with a varchar especially if you're going to index by that column. For one thing varchars take up half as much space as nvarchars, and for another thing nvarchar query plans use a scan, whereas varchars use a seek. Seek is much faster than scan. If that makes any sense.
blesh
What do I need nvarchar?
Mike C.
varchar only allows a single byte per character. "special" characters often require more than that.
Joel Coehoorn
But if I use the codes like © I will be fine with a varchar, right?
Mike C.
Maybe for a while, but eventually someone will try to put unicode characters into your app. best to be prepared.
Joel Coehoorn
Depends on your RDBMS. Some of them support UTF-8-encoded varchars. I don't think MS SQL Server does, though.
dan04
@dan04 Sql Server definitely does support UTF-8 (it just doesn't call it that)
Joel Coehoorn
+2  A: 

Well, with those characters, generally you will render them as

©

So you don't need to do anything special in the DB, but you should be using the "N"-prefixed fields for DB strings, NChar, NVarChar, and so on.

Noon Silk
Could you expand a little on the "N"-prefixed fields?
IrishChieftain
I'm just referring to these: http://msdn.microsoft.com/en-us/library/aa276823%28SQL.80%29.aspx
Noon Silk
Why should I use nvarchar?
Mike C.
Well, you need them if you want to hold international characters; i.e. a site/system that supports multiple languages, which most (significantly advanced) systems need to do; so it's a good precaution, IMHO.
Noon Silk
Thanks Silky - I may start a new question, but will these help me to cater for apostrophes and ampersands?
IrishChieftain
IrishChieftain: No need for those ones; they will fit in a regular "char" field.
Noon Silk
A: 

Just use the HTML entities, such as ™ and © if you can.

There's no need to switch to nvarchar unless you have an explicit requirement to support Unicode.

RickNZ
Great, thanks for the advice. I suppose this can cause difficulties if the same text field that contains these HTML entities is used in a windows applications instead of a web application.
Mike C.
Sure, depending on how the text is displayed. You could always use a browser window embedded in your Windows app, if you need to.
RickNZ
Why not just store the data as-is and convert it to HTML or JavaScript escapes as needed?
dan04
@dan04: if varchar can be used instead of nvarchar, the associated data will only occupy half the amount of space in the database. Also, the question was for an ASP.NET web page, in which case it's generally easier and more reliable to fixup the data once on write than to make sure that it's properly converted to escapes every place it's read.
RickNZ