Inserting Superscript Numbers in MS SQL

JeffB picture JeffB · Dec 14, 2012 · Viewed 10.5k times · Source

Trying to insert a value into a varchar data field with the Superscript number of 4 (power of 4) at the end.

I'm able to insert/update values using superscript 2 and 3 (ie squared and cubed), but I can't get the power of 4 to enter correctly into a varchar field?

It does work if I switch the field to a nvarchar, but I'm trying to avoid that.

This works for squared or to the power of 2

update mytable
set myfield = 'test'+NCHAR(0xb2)

However, trying to get this to work using a 4 at the end...

update mytable
set myfield = 'test'+NCHAR(0x2074)

It just updates it a numeric 4 and not superscript 4. Is this because the VarChar datatype recognizes squared and cubed, but not any other ones?

Answer

Scott Chapman picture Scott Chapman · Dec 14, 2012

Superscripts beyond 3 are only available as Unicode characters, so you need to use an NVARCHAR instead of a VARCHAR for your data field, unfortunately.

Please see the ASCII table for allowable characters without using NVARCHAR - You'll only see superscripts for 2 and 3.