Blog Post

Escaping strings

,

No, I’m not talking about that nightmare everyone has where a bunch of strings are chasing you across a loom.

Not everyone has that nightmare? Just me? Really? Um .. well .. You know, let’s just forget I said anything.

Escaping strings is an important concept when working with computers. Not just DBAs, database developers, or even application developers. If you work with strings then this is important for you.

Here’s the thing. Computers are amazing tools, but they are very literal. A computer does exactly what it’s told. That means that if you tell the computer that you’re going to put single quotes (‘) on either side of a string then the computer is going to take that first single quote and think begining a string and the very next single quote it’s going to think end of the string, even if that’s not what you intended.

‘Let’s take a look’

So what can we do to help the computer out? It really depends on the language. In T-SQL we double up on the character.

‘Let”s take a look’

In C# it’s a backslash.

‘Let\’s take a look’

In HTML we have to convert certain characters into special codes. (Which I’m not going to demonstrate since this is HTML and it would be a pain in the neck.)

So why is this so important? Setting variables is a good example.

DECLARE @LastName varchar(50) = 'O''Conner'

Again, this is important regardless of language and, honestly, regardless of job. If you work with strings you need to not only understand this concept, but you need to know how the particular language you are working with handles escaping.

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating