Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unusual T-SQL - What is happening here?

Can someone please explain to me what is going on with this query?

select 99.foo

It is behaving the same as

select 99 as foo

or

select foo = 99 

Also, it only appears to work with integers.

like image 295
Malk Avatar asked Feb 26 '23 03:02

Malk


2 Answers

The select 99.foo is translated as select 99. as foo (as you can leave out the space before the alias), selecting '99.' as a numeric. As there are no numbers after the decimal point, it just displays '99'.

You can confirm this by running:

select sql_variant_property(99., 'BaseType')

which returns numeric.

This is not the same as select 99 as foo, which does select '99', but as an int. This can be confirmed by runnning:

select sql_variant_property(99, 'BaseType')

which returns int.

Although all three queries appear to be the same, the first is different from the next two in the type of the value that is returned.

like image 152
adrianbanks Avatar answered Feb 27 '23 17:02

adrianbanks


The 'AS' is optional, so select 99 foo works.

The trailing period is interpreted as "point zero", so select 99. foo also works.

And there doesn't have to be a space between the value and the alias, so select .99foo works too.

These also work:

 select 99foo

 select 'ninetynine'foo
like image 35
Blorgbeard Avatar answered Feb 27 '23 18:02

Blorgbeard