October 19, 2015 by Kenneth Fisher
What do you do when you need to use a special character in a name? Well, I guess I should define what I mean by special first. Special characters are any non-alphanumeric character, so &#!~\/ etc, including spaces. It’s really pretty easy. Put s around the name. Now you can use “s if QUOTED_IDENTIFIER is set on but I wouldn’t recommend it. The s are more standard and you aren’t relying on a setting. FYI this is also how you manage names that are reserved words. I’m sure no one reading this has ever used a reserved word for a table name (for example) but you might work with someone who has and have to deal with the results.
One nice thing we can do with this is to create formatted column names. Say we have a query and we are planning on giving the output to our manager. It’s not worth doing anything special (ssis, ssrs) but we still want it to look nice.
SELECT name AS [Database Name], create_date AS [Date & Time Created] FROM sys.databases