30

I am trying to insert emoji / emoticons to a SQL Server database but it just stores ??? instead of the emoji / emoticons.

I am finding only help for SQL Server not MySQL.

I tried : link

but not finding answers even not able to set with :

ALTER TABLE mytable charset=utf8mb4, 
    MODIFY COLUMN textfield1 VARCHAR(255) CHARACTER SET utf8mb4,
    MODIFY COLUMN textfield2 VARCHAR(255) CHARACTER SET utf8mb4;

SQL Server does not recognize this command. This is only for Microsoft SQL Server not MySQL

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yagnesh
  • 1,123
  • 3
  • 17
  • 27

1 Answers1

53

Use NVARCHAR(size) datatype and prefix string literal with N:

CREATE TABLE #tab(col NVARCHAR(100));

INSERT INTO #tab(col) VALUES (N'      ⁉      ');

SELECT *
FROM #tab;

db<>fiddle demo

Output:

╔═════════════════════════════════╗
║              col                ║
╠═════════════════════════════════╣
║       ⁉      ║
╚═════════════════════════════════╝

EDIT:

SQL Server 2019 and forward supports UTF-8 collation:

CREATE TABLE t(col VARCHAR(100) COLLATE Latin1_General_100_CI_AI_SC_UTF8);
-- column's data type is VARCHAR!
-- collate could be set on column/database/instance level

INSERT INTO t(col) VALUES (N'☢️');

SELECT * FROM t;
-- col
-- ☢️

db<>fiddle demo - SQL Server 2019

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
  • it was mistake i was forgot to update database changes it works after updating entity – Yagnesh Dec 04 '15 at 12:04
  • @Yagnesh Please help me on it: I've a textbox in which user is able to type text like `Hi, This is test of emoji ☢️` and I've to save this record in table. Also once I pull this record from table and showing to client end must show same along with emoji. I've created column type as nvarchar(450) – dilipkumar1007 Feb 05 '23 at 02:27
  • 1
    It has to work, same way nothing to change – Yagnesh Feb 06 '23 at 04:13