Blog Post

Working with “special” names

,

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

Filed under: Microsoft SQL Server, SQLServerPedia Syndication, T-SQL Tagged: code language, language sql, microsoft sql server, T-SQL

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating