Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters (subscripts) not shown proprely

I want to put some chemical data into columns in the table. But in existing table subscripts are shown as normal characters. Some of them are shown as a question marks. What Am I supposed to do to fix it?

When I type this code

 SELECT N'H' + NCHAR(0x2082) + N'O'

It shows "H2O" properly

When I put it into existing table

 INSERT INTO tab (id, label)
 VALUES('100', N'H' + NCHAR(0x2082) + N'O')

It shows "H2O" not correctly

like image 764
Michael Carbol Avatar asked May 21 '26 18:05

Michael Carbol


1 Answers

you need to use NVARCHAR / NCHAR column in your table instead of VARCHAR / CHAR to store unicode characters.

In the example below, @table1, stores the formula correctly using NVARCHAR datatype whereas @table2 stores the same value in a VARCHAR column

DECLARE @table1 TABLE
(
    ID int,
    formula NVARCHAR(5)
)
DECLARE @table2 TABLE
(
    ID int,
    formula VARCHAR(5)
)

insert into @table1
SELECT '100', N'H' + NCHAR(0x2082) + N'O'

insert into @table2
SELECT '100', N'H' + NCHAR(0x2082) + N'O'

SELECT * FROM @table1;
SELECT * FROM @table2;
like image 72
ughai Avatar answered May 23 '26 07:05

ughai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!