Office applications and SQL Server Management Studio (SSMS) handle superscripts and subscripts differently. Office apps such as Word allow you to assign font properties to make a character appear superscripted or subscripted. SSMS doesn’t support font properties, but Unicode does provide special characters for common superscripting and subscripting symbols.
Before you get the green light to build that SQL Server database, you’ll first have to make a compelling case for it using Word or PowerPoint. You want your proposal to look slick, full of superscripts, subscript, and Greek math symbols. That’s where we’ll start. Consider the following:
water is H20 and it boils at 100 C
That’s not very impressive. We can subscript the 2 by selecting it and right-clicking to bring up the Font dialog box. Select Superscript or Subscript to make your formatting change.
Figure 1. Font dialog box in Office applications.
If you have a lot of subscripting and superscripting to do, using the Font dialog box to do the formatting takes too long. It’s faster and simpler to use keyboard shortcuts. Highlight the character(s) of interest and use Ctrl = to subscript, Shift Ctrl = to superscript. Just so there is no confusion, = is the key on which both + and = appear. (It’s possible someone may prefer to describe Shift Ctrl = as Ctrl + using the logic that Shift = is the same thing as the + key.)
But even with the subscripting done, we’re still not fully done. We need the degree symbol. I did a search using Unicode degree symbol as my search string and discovered that it is the Unicode character that is hexadecimal 00B0. I find that it is faster to search for the hex code using a search engine instead of bringing up the dialog box that shows all of the Unicode character set and scrolling until I see what I need.
Once you know the hex value for the symbol you need, go to the Insert tab in Word and then select Symbol. Use the Symbol dialog box to enter the hex value for the symbol of interest.
Figure 2. Select More Symbols… to specify your symbol by its hexadecimal value.
Figure 3. Enter the hexadecimal value for your symbol into the Character code box.
Now your text looks quite professional and complete (although a scientist would feel compelled to mention STP).
water is H20 and it boils at 100° C
To add subscripting, superscripting, and special characters in SSMS strings, string concatenation using the NCHAR function is necessary.
Figure 4. Building strings with the NCHAR function.
SELECT N'H' + NCHAR(0x2082) + N'O';
SELECT N'Ca' + NCHAR(0x207A) + NCHAR(0x207A);
SELECT N'E = mc' + NCHAR(0x00B2);
SELECT N'98.6' + NCHAR(0x00B0) + N'F';
SELECT NCHAR(0x03B2) + N'-blocker';
SELECT N'X ' + NCHAR(0x2265) + N' 6.0221415 * 10' + NCHAR(0x00B2) + NCHAR(0x00B3);
Try the preceding strings for yourself. The inverse of the NCHAR function is the UNICODE function, which returns a decimal value. You can use either decimal values or hexadecimal values with the NCHAR function. I showed mostly hexadecimal examples because I think you’ll most often find hex values when you are looking up the numeric value of a Unicode special character.
Figure 5. Working with decimal values.
SELECT N'H' + NCHAR(8322) + N'O';
NOTE: Some of the code samples may not render correctly depending on the capabilities and settings of your browser. Thanks to my fellow SQL Server MVPs Greg Low and Rob Farley for technical assistance in writing this post.
Using Unicode characters for superscripting and subscripting is particularly useful when markup languages can’t be used, such when posting in some social media platforms.