Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL Dollar Sign In Expressions

In this answer, there is a trick which allows to use the ROW_NUMBER() windowed function with a 'constant' in the ORDER BY clause:

SELECT ROW_NUMBER() OVER (ORDER BY $/0) 
FROM master..spt_values 

After some search in Google, I can't find what dollar sign means in this context?

I've tried to execute a simple query:

SELECT $;

And it returns 0.

Could somebody explain this?

like image 923
Alexander Abakumov Avatar asked May 20 '15 09:05

Alexander Abakumov


People also ask

How do I insert a dollar sign in SQL?

In SQL Server, you can use the T-SQL FORMAT() function to format a number as a currency.

What are T-SQL commands?

T-SQL (Transact-SQL) is a set of programming extensions from Sybase and Microsoft that add several features to the Structured Query Language (SQL), including transaction control, exception and error handling, row processing and declared variables.

What is format function in SQL?

Definition and Usage. The FORMAT() function formats a value with the specified format (and an optional culture in SQL Server 2017). Use the FORMAT() function to format date/time values and number values. For general data type conversions, use CAST() or CONVERT().

What is data type of time in SQL?

The most widely used one is the DATETIME as it has been present since the earlier versions of SQL. SQL retrieves and displays DATETIME values in 'YYYY-MM-DD hh: mm: ss' format. The supported range is '1753-01-01 00:00:00' to '9999-12-31 23:59:59.997'. Let's explore datetime type in more detail.


2 Answers

It's just a money constant (what T-SQL calls literals).

You presumably would have been less surprised if you saw the expression $2.50, which would just be another constant.

Some other examples: select £,¢,¤,¥,€ all return 0s also.


It can be tricky to determine what type of data you're looking at in T-SQL. One trick, if you suspect you know what type it is is to pick an incompatible type and attempt the conversion:

select CONVERT(date,$)

Result:

Msg 529, Level 16, State 2, Line 1
Explicit conversion from data type money to date is not allowed.
like image 90
Damien_The_Unbeliever Avatar answered Sep 20 '22 12:09

Damien_The_Unbeliever


Thanks to @Damien for pointing to the right direction.

I'd just like to add to his answer results of a query that gives an exact description what are constants with the $ sign:

  SELECT
    $ AS Value,
    SQL_VARIANT_PROPERTY ( $ , 'BaseType' ) AS BaseType,
    SQL_VARIANT_PROPERTY ( $ , 'Precision' ) AS Precision,
    SQL_VARIANT_PROPERTY ( $ , 'Scale' ) AS Scale,
    SQL_VARIANT_PROPERTY ( $ , 'TotalBytes' ) AS TotalBytes,
    SQL_VARIANT_PROPERTY ( $ , 'MaxLength' ) AS MaxLength
  UNION ALL
  SELECT
    $2.50,
    SQL_VARIANT_PROPERTY ( $2.50 , 'BaseType' ),
    SQL_VARIANT_PROPERTY ( $2.50 , 'Precision' ),
    SQL_VARIANT_PROPERTY ( $2.50 , 'Scale' ),
    SQL_VARIANT_PROPERTY ( $2.50 , 'TotalBytes' ),
    SQL_VARIANT_PROPERTY ( $2.50 , 'MaxLength' )

Results:

Value   BaseType    Precision   Scale   TotalBytes  MaxLength  
0.00    money       19          4       10          8  
2.50    money       19          4       10          8
like image 45
Alexander Abakumov Avatar answered Sep 20 '22 12:09

Alexander Abakumov