"The first shall be last, and the last shall be first"
REVERSE(STUFF ( REVERSE(lastname), 1, 1, ''))
It seems that this behavior / ability is both known and deprecated. I was looking through the sys.dm_os_performance_counters DMV the other day and noticed the following two entries:
object_name counter_name instance_name
----------------------------- ------------ -------------------------------
SQLServer:Deprecated Features Usage '@' and names that start with '@@' as
Transact-SQL identifiers
SQLServer:Deprecated Features Usage '#' and '##' as the name of temporary
tables and stored procedures
I then checked the MSDN documentation and found both of these noted on the Deprecated Database Engine Features in SQL Server 2016 page, in the Features Not Supported in a Future Version of SQL Server section, under the Category of "Transact-SQL":
Deprecated feature Replacement Feature ID
----------------------------------- -------------------------------------- ----------
Use of #, ## as temporary table and Use at least one additional character. 185
temporary stored procedure names.
Use of @, @@, or @@ as Do not use @ or @@ or names that begin 186
Transact-SQL identifiers. with @@ as identifiers.
The earliest reference I could find for this deprecation notice was in the SQL Server 2008 documentation. And while the version drop-down on that "Deprecated Database Engine Features" page does not have an entry for SQL Server 2005, you can still get to that documentation via, https://msdn.microsoft.com/en-us/library/ms143729(v=sql.90).aspx, and see that neither of these items are listed.
That information leads me to the following conclusions:
Question Part 1 (is this a bug or intended behavior):
It is intended, albeit undesirable, behavior.
Question Part 2 (is there a benefit to this behavior):
Not only does there not seem to be any beneficial use-case here, even if there was, it wouldn't matter too much due to this behavior / ability being deprecated and hence not something that should be used going forward, especially in new code / projects.
Best Answer
One option is using a CASE statement, such as this one: