Escaping strings

Kenneth Fisher, 2018-09-24

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.





Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

Robert Davis


1,567 reads

Networking – Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I’d like to talk about social networking. We’ll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let…

Andy Warren


1,530 reads

Speaking at Community Events – More Thoughts

Last week I posted Speaking at Community Events – Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I’ve got a few more thoughts on the topic this week, and I look forward to your comments.

Andy Warren


360 reads