What is the usage of '\' and '$' in T-SQL?

一个人想着一个人 提交于 2021-02-05 04:48:56

问题


As I found (in SQL Server books):

\ (Backslash) (Transact-SQL)
Breaks a long string constant into two or more lines for readability.

and

SELECT Clause (Transact-SQL) ... $IDENTITY | $ROWGUID
And
$PARTITION (Transact-SQL)
Returns the partition number into which a set of partitioning column values would be mapped for any specified partition function.

of usage of \ and $ in T-SQL specially SQL Server.

Now, I have a query like this:

SELECT \ a, $ b, \11 c, $12 d;

That have a valid result like this:

a    | b    | c     | d 
-----+------+-------+-------
0.00 | 0.00 | 11.00 | 12.00

I think there is something that I can't find it about this characters.

Edit :
I found that if a number comes after currency symbols, SQL Server will remove the defined symbol and store the value as a money data type:

And I think, SQL Server translate a single currency symbol that is the last phrase in a part -these parts are between + and -- of formula to 0.00 and only at the end of the part like -$, ($), (12 + $), ($) + 12, $ * (12 - $) or so on, and not $ + 1, 2 * $ - 1. Also I found $ 2 is same as $2.

All above behavior is same for \ that means SQL Server thinks \ is a currency symbol!!!


回答1:


I thought to check the datatype and each as you'll notice each returns the datatype money.

WITH CTE
AS
(
    SELECT \ a, $ b, \11 c, $12 d   
)

SELECT  SQL_VARIANT_PROPERTY(a,'baseType') a,
        SQL_VARIANT_PROPERTY(b,'baseType') b,
        SQL_VARIANT_PROPERTY(c,'baseType') c,
        SQL_VARIANT_PROPERTY(d,'baseType') d
FROM CTE

Results:

a                              b                              c                              d
------------------------------ ------------------------------ ------------------------------ ------------------------------
money                          money                          money                          money


来源:https://stackoverflow.com/questions/30287368/what-is-the-usage-of-and-in-t-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!