Some interesting, obscure (and absolutely useless) T-SQL syntax

Comments 0

Share to social media

/*
Writing ugly SQL Statements is an art. Today I’ve spent
a few minutes trying to break our parser as well as giving
a grill to SQL Server’s parser.
So here is some extra syntax that is not behaving as
I’d expect it to behave.
*/

select 1.a

— Yes, this is legal, surprise surprise it returns a
— table with a single column, a single row, the value
— is 1, the column name is “a” !

— So let’s push it a bit further:

select -1.a

/*
Works, the result is:
a
—————————————
-1
*/

select -1/2.[-1/2]

/*
Wow, this works too, and it gives me
-1/2
—————————————
-0.500000

So can I just put anything after the ?decimal? point?
*/

select 1.SomeString

/*
SomeString
—————————————
1
*/

— But then there are exceptions:
select 1.a, 1.e, 0.a

/*
a                                  a
———– ———————- —————-
1           1                      0.0
sa

1.e does not produce the alias (e for exponent???),
0.a seems to use float, so the value displayed is 0.0

It is a shame that you cannot use this with string literals.
Unless you want to torture your colleagues I reckon that
the above is absolutely useless. But if you have an idea why
the above works, and works the way it works, do let me know.

PS: the above syntax works on 2000,2005 and 2008.

  Andras
*/

Load comments

About the author

András Belokosztolszki

See Profile

András Belokosztolszki is a software architect at Red Gate Software Ltd. He is a frequent speaker at many UK user groups and events (VBUG, NxtGen, Developer’s Group, SQLBits). He is primarily interested in database internals and database change management. At Red Gate he has designed and led the development of many database tools that compare database schemata and enable source control for databases (SQL Compare versions 4 to 7), refactor databases (SQL Refactor) and show the history of databases by analyzing the transaction log (SQL Log Rescue). András has a PhD from Cambridge and an MSc and BSc from ELTE, Hungary. He is also a MCSD and MCPD Enterprise. See my blogs on simple-talk.

András Belokosztolszki's contributions