views:

52

answers:

2

I have used to read that varchar (char) is used for storing ASCII characters with 1 bute per character while nvarchar (varchar) uses UNICODE with 2 bytes.
But which ASCII? In SSMS 2008 R2

DECLARE @temp VARCHAR(3); --CHAR(3)   
SET @temp = 'ЮЯç'; --cyryllic + portuguese-specific letters
select @temp,datalength(@temp) 
-- results in 
-- ЮЯç  3

Update: Ooops, the result was really ЮЯс but not ЮЯç. Thanks, Martin

+1  A: 

It's ASCII with a codepage which defines the upper 128 characters (128-255). This is controlled by the "collation" in SQL Server, and depending on the collation you use you can use a subset of "special" characters.

See this MSDN page.

Lucero
+1 thanks. It is ubiquitous to read about ASCII used by CHAR (VARCHAR) though they really use extended ASCII from Windows.
vgv8
+3  A: 
declare @table table
(
c1 char(4) collate Cyrillic_General_CS_AI,
c2 char(4) collate Latin1_General_100_CS_AS_WS
)

INSERT INTO @table  VALUES (N'ЮЯçæ', N'ЮЯçæ')

SELECT c1,cast(c1 as binary(4)) as c1bin, c2, cast(c2 as binary(4)) as c2bin
FROM @table

Returns

c1   c1bin      c2   c2bin
---- ---------- ---- ----------
ЮЯc? 0xDEDF633F ??çæ 0x3F3FE7E6

You can see that dependant upon the collation non ASCII characters can get lost or silently converted to near equivalents.

Martin Smith