I am using a SQL Server 2005/2008 Express database. Are there any problems with using the N string prefix (used for nvarchar fields) for varchar fields?
e.g. if I have a database field:
CREATE TABLE [dbo].[posts](
post_title varchar(30)
)
And then I insert just ascii data but with an N prefix:
INSERT INTO [dbo].[posts] ([post_title]) VALUES (N'My Title');
The problem arises because I want to save UTF-8 characters from a PHP application and I can’t currently differentiate whether the field it is being saved to is varchar or nvarchar. So I just want to assume that all are nvarchar given that I will only ever try to save ASCII characters to varchar fields.
If you write strings with the N prefix into a varchar field it will be implicitly converted. There is no other overhead and you can safely assume “everything is nvarchar”
There may be an problem comparing nvarchar variables to varchar columns because of data type precedence. The varchar column will be converted and any indexes won’t be used.