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?
In SQL Server, you can use the T-SQL FORMAT() function to format a number as a currency.
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.
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().
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With