varchar and nvarchar, which one to use???
From: === Steve L === (steve.lin_at_powells.com)
Date: 12/24/04
- Next message: Jeff Lynch: "Help with Query"
- Previous message: David Portas: "Re: Tricky CASE statement"
- Next in thread: Andrew J. Kelly: "Re: varchar and nvarchar, which one to use???"
- Reply: Andrew J. Kelly: "Re: varchar and nvarchar, which one to use???"
- Reply: Erland Sommarskog: "Re: varchar and nvarchar, which one to use???"
- Messages sorted by: [ date ] [ thread ]
Date: 24 Dec 2004 14:39:54 -0800
i'm reading the section of an article at this site
<<< http://www.sommarskog.se/dyn-search.html>>>
and i was confused the correct use of varchar and nvarchar.
I've been using varchar in the past, but i noticed all the code samples
use
something like this:
DECLARE @sql nvarchar(4000)
SELECT @sql = 'SELECT * FROM orders'
what's the best practice using these data types?
for a business like amazon.com as an example, what should be used in
the database and in the stored procedures? varchar or nvarchar?
can someone educate me on this in a more straight foward fashion?
thank you!
steve
=== excerpt from article ====
The Northwind database consistently uses the nvarchar data type, but
I've failed to handle this in my procedure. The data in Northwind
appears to be restricted to the characters in Windows Latin-1, which
covers languages such as English, Spanish, French and German, so if you
have a system collation based on this character set, you would never
get any incorrect response from search_orders_2. However, a Russian
user trying:
EXEC search_orders_2 @city = N'Bräcke'
would not get the orders from Folk och Fä HB in Bräcke, because
Bräcke would be converted to varchar as Bracke. This is how my
procedure should have read: (lines 70-71)
SELECT @sql2 = @sql2 + N' AND c.City = N' +
quotename(@city, '''')
And that N which starts an nvarchar literal, should appear with all
nvarchar parameters. (The N stands for National, and is taken from the
ANSI standard SQL-92.)
So why did I not give you the proper code? Well, I figured that many
readers would take the code as a template for their own code, and I
don't want you to include that N by routine. Because, if you use a
nvarchar literal together with an indexed varchar column, the varchar
column will be converted to nvarchar, and that precludes use of the
index. Thus, you would get the right result, but performance would be
poor.
- Next message: Jeff Lynch: "Help with Query"
- Previous message: David Portas: "Re: Tricky CASE statement"
- Next in thread: Andrew J. Kelly: "Re: varchar and nvarchar, which one to use???"
- Reply: Andrew J. Kelly: "Re: varchar and nvarchar, which one to use???"
- Reply: Erland Sommarskog: "Re: varchar and nvarchar, which one to use???"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|