Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Symbols are being displayed as a question mark in SQL Server Database

Today i noticed that html symbols such as: ★ are being displayed in my database as a question mark.

I'm using varchar as type and the database i am using is microsoft sql 2008.

Does anyone know a fix for this?

like image 660
Jamie Avatar asked Dec 31 '13 15:12

Jamie


1 Answers

You need to use NVARCHAR datatype for your column, VARCHAR datatype can only be use for non-unicode character.

if you are storing unicode characters in your datatype you should use NVARCHAR datatypes and when inserting Data into your Column use the N prefix telling sql server there will be some unicode characters in the passed string.

With VARCHAR DataType

CREATE TABLE #Temp (Column1 VARCHAR(100))
INSERT INTO #Temp VALUES('★')
SELECT * FROM #Temp

Result

╔═════════╗
║ Column1 ║
╠═════════╣
║ ?       ║
╚═════════╝

With NVARCHAR DataType

CREATE TABLE  #Tempn (Column1 NVARCHAR(100) )
INSERT INTO #Tempn VALUES(N'★')        --<-- N prefix for Unicode Characters
SELECT * FROM #Tempn

Result

╔═════════╗
║ Column1 ║
╠═════════╣
║ ★       ║
╚═════════╝
like image 164
M.Ali Avatar answered Oct 03 '22 07:10

M.Ali