QUOTENAME Basics: #SQLNewBlogger

Recently I ran across some code that used a lot of QUOTENAME() calls. A colleague was having some trouble with the code, but what struck me was that I hadn’t often delved into the details of QUOTENAME and how it can be used in different ways. I’d always just passed in a string as a single parameter.

This post looks at a few details of how this function works.

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

QUOTENAME

The idea behind QUOTENAME() is that you pass in a string that might not be properly formatted to be an indentifier. QUOTENAME() returns the string with enclosing characters that ensure the string works in your code as a literal.

Here’s an example. If I have a string that is “Steve Jones”, I’ve quoted the string with double quotes. If I wanted to create a table with this string, I’d do this:

CREATE TABLE dbo.[Steve Jones] (id int);

I’ve explicitly put brackets around the string, which is what we commonly do in SQL Server if we have some reserved word or space we want in the object name.

NOTE: I am not recommending this, just showing this as an example.

If I were doing this in code, and maybe I wanted to dynamically create this table, I’d do this:

DECLARE @n nvarchar(20) = N'Steve Jones';
DECLARE @s nVARCHAR(100) 

SELECT @s = 'create table dbo.' + QUOTENAME(@n) + '(id int)'

EXEC(@s)

When QUOTENAME runs, by default, it will surround the string with brackets. You can see this in the results below.

2026-05_0281

This is a valid identifier, and we end up with a table that has a space in it’s name, which I abhor. But it works.

A Second Parameter

While this is how I’ve used QUOTENAME in the past, usually to clean up strings that might be reserved words, like name, there actually is an optional second parameter. The syntax for QUOTENAME is:

QUOTENAME ( string, [ character])

where

  • string – the string that you need to quote
  • character – a single character that represents the delimiters to be used to surround the string.

One might think that any character can be used, but that’s not true. Only a few characters are supported. The list is:

  • brackets, [], which is the default
  • braces, {}
  • single quotes, ‘
  • double quotes, “
  • angle brackets, <>

That’s it. Anything else produces not an error but a NULL, as shown here:

2026-05_0282

What’s interesting is that the parameter is a single character, but the function works out what the matching character should be. For single and double quotes, this is easy. The same character is used, as you can see below. Note the single quote is escaped.

2026-05_0283

For brackets, if I use either the left or right bracket, the result has a left bracket on the left side and a right bracket on the right side. You can see that below.

2026-05_0284

Same thing for braces.

2026-05_0285

And angle brackets.

2026-05_0286

What’s what I’d expect, but it’s nice to know it works. This limits flexibility for the function, and if I were designing it, I might make the second parameter two characters that represent the left and right enclosures. Or make a separate parameter for each. That would allow me to do something like:

SELECT QUOTENAME(‘Steve Jones’, ‘_>’ )

and get

_Steve Jones>

I could have run with a space at the beginning and a comma or period at the end, helping me clean up text. Winking smile

SQL New Blogger

This is a quick post that I actually spent about 10 minutes on during a flight. I had run into this while answering a friend and reading the docs, so I left a quick sentence as a reminder and then fleshed out this post. I spent another 10 minutes once I landed (and got plugged in) capturing the screen shots.

This is a good example of showing how I dug into a feature of SQL Server, I understand how it works, show how it can be used, and how I might have wished it would be used.

You could do this in a half hour at a coffee shop and start knowing that you can learn a few things and maybe show how you’d evaluate if this was needed in AI generated code, some of which you might see in the very near future.

Posted in Blog | Tagged , , | 2 Comments

What Can AI Really Do?

I wonder how many of you have tried vibe coding something with an AI tool. If you haven’t, I certainly recommend it. I’ve been a bit amazed with a few of my AI Experiments, including my loading of a lot of inconsistently formatted data into a database for USD$5.

To be clear, there’s plenty of vibe coding that might not be production-ready, but have you ever been handed code from a human developer you didn’t think was production-ready? Or deployed code like that? Certainly, AI could exacerbate the situation, but it can also spark ideas, ease (and speed) development in small ways, and tackle the backlog of things your org needs.

Especially small tools.

How big a concern or help will this be? I ran across an interesting article from a semi-technical person trying to build a text analysis tool. This is the type of thing we may do as database pros, but we wouldn’t have time to service every request for this assistance. There is a mixed bag of success in the piece, and a recognition that software developers have skills and knowledge that AI tools can’t necessarily duplicate in the hands of a non-pro.

However, that’s where I think software engineers and database professionals have to learn to leverage AI tools to become more efficient, prove their worth, and, honestly, get more done without working longer hours. It’s also a place where you might guide users in producing some useful, but less mission critical software for themselves with AI. That might lower the number of requests I get.

To me, that’s the direction I want to go with AI. More productive, less stress, and the same (or fewer) hours.

What do you think AI can do for you? Let us know today.

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged | 1 Comment

Breaking Rules in Rome

Last week I was honored with a trip to Rome for the Redgate President’s Club. I was awarded this, along with our top people in Sales, for the work I did in 2025. It was a fun trip, but as we prepared to depart Cambridge, one of the rules our Chief Revenue Officer gave us was:

Don’t work this week

She told us that the company should be able to survive a week without us. On Wednesday, I got reminded of this when I replied to a few Slack messages.

Thursday I got reminded again.

By Friday she had given up.

It’s not that I don’t want to get away from work, or don’t, but there are things that come up and can be easily dealt with. In both cases, I had people ping me about things that are happening this week (18May) or in two weeks (1 Jun), and I need to ensure I’m prepped. I also had responses on a couple of SQL Server Central tickets, that I needed to provide a few details on.

These were small things, and I wasn’t checking email or most Slack messages, only a few channels where I knew there would be some relevant activity. I also scanned email looking for only ticket responses so I could provide info if needed.

Sometimes work takes priority over other things. I’ve had to handle a few things on sabbaticals. I’ve had to respond on vacation for certain things. In general I tell people to leave me alone, and they do for the most part.

I’m not upset, and it’s the price of being successful and involved. Finding this balance is important, and it’s easy to work too much, or too little. I feel I’ve got a pretty good balance when I go on vacation, minimizing interruptions, though certainly not eliminating them.

That being said, I did have a good time. Not sure why I’m not smiling, but the Colosseum was amazing.

20260515_182019

Posted in Blog | Tagged , | 2 Comments

Republish: Writing Before Reading

I’m still in Europe, now in Rome. I’m sure it’s amazing, but while I’m gone, you get Writing Before Reading. I don’t see this in the archive, so here it is in total:

Writing Before Reading

This editorial was originally published on May 21, 2020. It is being republished as Steve is gone this week. Have your thoughts changed on reading and writing code?

Coding is one of the few (or maybe only) places where we learn to write a language before we learn to read it. Many of us get started in some way by working through exercises, looking at very small snippets of code and then starting to write our own. Perhaps that’s because we experiment by changing the behavior of the computer, which naturally takes some writing work on our part.

Perhaps we can do better, and should to better. Perhaps we would become less susceptible to the Not Invented Here (NIH) stance that so many developers seem to take. Not with major projects, but often with small sections of code or libraries, preferring to structure something new than re-used (and take the time to understand) what’s already there.

This isn’t to imply that most developers need to write their own code or don’t work to understand others, but often they prefer to rebuild something in a way that is more familiar. It’s a form of the Mere-exposure effect, and it sometimes leads developers to prefer certain patterns, tools, platforms, and more. Perhaps it’s the opposite of the shiny ball syndrome, which leads people to chase the newest thing available in their area.

I know there are plenty of developers that copy and paste code from the Internet as well, which might seem to be a way of getting around NIH syndrome, but I think many people do this when they find code that solves a problem. Even if they don’t understand how it works, they’ll seek to reapply that same code to a new problem.

I do think reading code is hard. In some sense, it sucks. I’d often prefer to understand the basic inputs and outputs, perhaps rewriting the code to work in a way that’s more comfortable for me to understand. Maybe I better understand my own code, maybe I’m falling victim to the IKEA effect (I’d rather depend on my own software than someone else’s), or maybe I’m just being lazy.

There are plenty of articles on how to read other people’s code, and I am trying to get better at this. I do try to debug and reason out how queries are written that solve problems in the forums. I may break apart queries and try to understand how someone has used a window function or a CROSS APPLY in a certain way. It helps build my skills, but it also helps me learn to better read other people’s code and perhaps carry that skill into the future.

Steve Jones

Posted in Editorial | Tagged | Leave a comment