Escaping strings

Leave a comment

September 24, 2018 by Kenneth Fisher

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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,755 other subscribers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: