Andras

Software Architect - Red Gate Software

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

Published Wednesday, November 28, 2007 10:05 AM

/*
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
*/

by András

Comments

 

GSquared said:

I did a few tests, and it's a method call per:

select '1'.e

error: Cannot call methods on varchar

The decimal otherwise works like a sort of limited subset of the "as" clause.  For example:

select 1.[e]
select 1.e

The one with braces on it gives "e" as the column name, while the other gives no column name (as per your original question).

I looked in BoL, and it has this to say in the syntax on SELECT:

"{ . | :: }
Specifies a method, property, or field of a CLR user-defined type. Use . for an instance (nonstatic) method, property, or field. Use :: for a static method, property, or field. To invoke a method, property, or field of a CLR user-defined type, you must have EXECUTE permission on the type."

While that doesn't explain why "e" doesn't give a column name like the other versions I tested, it gives what I think is a clue.

Since I couldn't easily find any documentation on this behavior, I don't have a definite answer, but it looks to me like calling it this way calls a method, in which "e" (and possibly other strings, I didn't try very many), has an explicit use, while "a" (and others tested) have an implicit use as part of an "as" method.
November 28, 2007 9:46 AM
 

cataclysm said:

The full spec for a float is  int [. [int ['e' int]]]

So 1.e is interpreted as 1.0e0 (which is 1)
You can confirm this with 1.e1, which gives 10.
January 18, 2008 9:31 AM
 

brianh123 said:

This is nothing obscure or complicated, nor is it a CLR method call. Try this:
   SELECT 1 a
You'll get a single row with a single column "a" and value 1. This is because AS is optional.

Try this:
  SELECT 1. a
Again, you'll get the same result. "." is simply a decimal point, which is redundant after an integer.

Omit the space, and you get the example in the article:
  SELECT 1.a
The parser doesn't need the space because it knows that a letter cannot follow a decimal point, but this is interpreted as "SELECT 1. AS a".
February 16, 2008 11:37 AM
You need to sign in to comment on this blog

About András

András Belokosztolszki is the architect of SQL Compare versions 4, 5, 6 and 7, SQL Log Rescue and SQL Refactor. He is focused on database internals, database synchronization and database schema evolution.

















<November 2007>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
Reporting on Mobile Device Activity Using Exchange 2007 ActiveSync Logs
 In this new column giving practical advice on all things Sys Admin related, Ben Lye takes on the often... Read more...

The Bejeweled Puzzle in SQL
 Alex Kozak provides another SQL puzzle to hone your SQL Skills with.  Read more...

Using Powershell to Generate Table-Creation Scripts
 For all of us who learn best by trying out examples, Bob Sheldon produces a PowerShell script file for... Read more...

Configuring Exchange Server 2007 to Support Information Rights Management
 In Exchange Server 2007, Information Rights management is easy to set up once you have set up the... Read more...

SQL Response: The dim sum interview
 Richard Morris met David and Nigel of the SQL Response team, in a dim sum Restaurant in Cambridge. They... Read more...