Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add emoji / emoticon to SQL Server table

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

like image 354
Yagnesh Avatar asked Nov 26 '15 12:11

Yagnesh


People also ask

How do I add emoji to SQL?

Absolutely it is possible to create your database with any emoji or even display the emoji in your SSMS (SQL Server Management Studio)'s result tab. The simplest possible way is to just type in emoji in the SSMS query window and run the query. It will display the image in the result tab.

Can you store emoji in database?

Some emojis such as āš” can be stored in MySQL's 3-byte utf8 encoding, but others such as face screaming in fear cannot.

When would you use the Nvarchar data type within a table?

Use nvarchar when the sizes of the column data entries vary considerably. Use nvarchar(max) when the sizes of the column data entries vary considerably, and the string length might exceed 4,000 byte-pairs.


1 Answers

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

like image 151
Lukasz Szozda Avatar answered Sep 20 '22 18:09

Lukasz Szozda