HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Avoiding "?" symbols in string column

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
columnavoidingstringsymbols

Problem

In my contact table, I'm getting ? symbols in LastName. How can I avoid that?

FirstName   MiddleName     LastName
Bjorn       Erik           N?ss
Gunn                       Gjos?ther
Oystein     Larsen         Indrev?r
Louise      Tulle          Pr?strud

Solution

You need to ensure the LastName column is a Unicode type, for example nvarchar(30), and use the N prefix on any literal strings you use in insert/update/merge statements. (Alternatively, you could use a non-Unicode type with a code page that supports all the characters you need.)

SQL Server replaces characters it cannot convert with the ? symbol.

See nchar and nvarchar (Transact-SQL)


Prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. This default code page may not recognize certain characters.

The code page is determined by the collation. Taking SQL_Latin1_General_CP1251_CI_AS as an example:

SELECT FH.[description]
FROM sys.fn_helpcollations() AS FH
WHERE FH.name = N'SQL_Latin1_General_CP1251_CI_AS';


Output:

Latin1-General, case-insensitive, accent-sensitive,
kanatype-insensitive, width-insensitive for Unicode Data
SQL Server Sort Order 106 on Code Page 1251 for non-Unicode Data


Code page 1251 (used for non-Unicode data in that collation) does not have the æ character, so:

SELECT 'Gjøsæther' COLLATE SQL_Latin1_General_CP1251_CI_AS;


Produces:

Gjos?ther


Notice that as well as the ?, the ø character was translated to o.

The question does not state which collation the instance/database/column is using, but it seems very likely it is the wrong one for your data.

More general information:

Data Types (Transact-SQL)

Collation and Unicode Support

Code Snippets

SELECT FH.[description]
FROM sys.fn_helpcollations() AS FH
WHERE FH.name = N'SQL_Latin1_General_CP1251_CI_AS';
SELECT 'Gjøsæther' COLLATE SQL_Latin1_General_CP1251_CI_AS;

Context

StackExchange Database Administrators Q#132251, answer score: 12

Revisions (0)

No revisions yet.