T-SQL 101: #41 Using string literals in SQL Server

When we write string literal values, we need to surround them in single quotes. There are options in some SQL products we could make that double quotes. In general though, don't do that. Just use single quotes.

If you have multi byte characters, they need to be preceded by the N prefix. I've previously mentioned that it stands for National Character Set. If I don't use the N, when I execute the second query, I'd see this:

My multi-byte Chinese characters get replaced by question marks.

However, if I include the N, I see this:

Without the N, we just get the ANSI characters, basically our normal English alphabet and numbers and punctuation and so on. The minute you need to use a language that has way more characters than we do, then you can't just use single quotes or the values won't be interpreted correctly.

One other thing worth noting is that sometimes you need to put a quote in the middle of a string. To do that, you use two quotes like this:

Notice that when I execute this, only one quote is output.

Learning T-SQL

It's worth your while becoming proficient in SQL. If you'd like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.

Leave a Reply

Your email address will not be published. Required fields are marked *