Is It A String Literal or An Alias
Is It A String Literal or An Alias
<< Working with Date and/or Time values in SQL Server: Don't Format, Don't Convert -- just use
DATETIME | Home | SQL Server 2008 - Enhancements in Date and Time Data Types (link) >>
Every now and then I see T-SQL code written like this:
Notice that 'columnname', despite its appearance, is not a string literal or a string
expression, it is the alias that we are assigning to the column somecolumn. Putting the
name of an alias in single quotes in T-SQL is completely valid syntax, and it will work
fine, but I feel that it is a really bad practice. It makes the distinction between string
literals and object names very blurry, and it can lead to confusion when examining and
maintaining your code.
columnname
----------
literal
(1 row(s) affected)
It just makes your code harder to read, especially if syntax highlighting is used, since
we no longer know at a glance what is a string literal and what is not. Of course, it gets
even more difficult when you are dealing with long, complicated SQL statements.
It gets even worse when you consider that using "as" is optional when assigning an
alias. For example, this is perfectly legal as well:
columnname
----------
literal
(1 row(s) affected)
I don't know about you, but I would hate to inherit a database full of code like that!
http://weblogs.sqlteam.com/jeffs/archive/2007/08/30/string-literal-alias-sql-server.aspx 1/4
11/22/2017 Is it a String Literal or an Alias?
Allowing the string literal delimiter to be used also implies to beginners that variables or
other expressions can be used in place of column aliases, since they look like string
expressions. Perhaps that is why now and then we see code like this:
That would of course make the misguided "generate dynamic column names" people
very happy, but thankfully is not allowed.
Interestingly, while you can write column name aliases with the single quotes, you
cannot alias tables or derived tables that way:
select * from
(
select 'jeff' as colname
) as 'test'
My advice? Don't delimit column aliases the same way you delimit string literals --
either do not delimit them at all, or use square brackets like [this]. This will help to
distinguish your data from your code, and make things a little easier to maintain in the
long run.
see also:
Related Links
Print | posted on Thursday, August 30, 2007 11:36 AM | Filed Under [ Miscellaneous T-SQL ]
http://weblogs.sqlteam.com/jeffs/archive/2007/08/30/string-literal-alias-sql-server.aspx 2/4
11/22/2017 Is it a String Literal or an Alias?
Feedback
+1 on that one!
georgev -- I personally don't use that, not sure exactly why, maybe just out of
habit. I think that it's fine as long as you are consistent. It can make your code
more readable in that you can line up your column names on the left ...
I need to specify a string literal in a where clause, but it is also the name of
one of the fields.
How do I specify the string literal so that it is not confused with the field of the
same name?
http://weblogs.sqlteam.com/jeffs/archive/2007/08/30/string-literal-alias-sql-server.aspx 3/4
11/22/2017 Is it a String Literal or an Alias?
AND
vc_cs.vc_code = ''
Here though, the table valid_codes also has a field called [purchasestatus]
which is making the search fail. The field name cannot be changed nor the
domain field text.
I have tried double quotes also but then it fails with Invalid column name.
4/29/2008 11:34 AM | ow
http://weblogs.sqlteam.com/jeffs/archive/2007/08/30/string-literal-alias-sql-server.aspx 4/4