SQL SELECT $(美元符号)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30406934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:39:55  来源:igfitidea点击:

SELECT $ (dollar sign)

sqlsql-serverselect

提问by Hussein Fawzy

I have good experience in SQL Server, But suddenly I discovered this strange SELECTcommand

我在 SQL Server 方面有很好的经验,但突然发现了这个奇怪的SELECT命令

SELECT $ 

or

或者

SELECT $ FROM tableName

All the time it returns zero scalar value (0.00), or a new column with all values of 0.00

0.00始终返回零标量值 ( ),或所有值为0.00

What is that?

那是什么?

采纳答案by Icemanind

When SQL Server comes across your $sign, it automatically converts it into a moneydata type. Because you don't have an explicit value after the dollar sign, SQL Server is assuming 0.00. From MSDN:

当 SQL Server 遇到您的$标志时,它会自动将其转换为money数据类型。因为美元符号后没有明确的值,所以 SQL Server 假设0.00. 从MSDN

When converting to moneyor smallmoney, integers are assumed to be monetary units. For example, the integer value of 4 is converted to the money equivalent of 4 dollars (for us_english, the default language). Numbers to the right of the decimal in floating-point values are rounded to four decimal places for money values. Expressions of data types char or varchar that are being converted to an integer data type must consist only of digits and an optional plus or minus sign (+ or -). Leading blanks are ignored. Expressions of data types char or varchar converted to money can also include an optional decimal point and leading dollar sign ($).

当转换为moneyor 时smallmoney,整数被假定为货币单位。例如,整数值 4 转换为 4 美元等值的货币(对于 us_english,默认语言)。浮点值中小数点右侧的数字四舍五入到小数点后四位的货币值。转换为整数数据类型的数据类型 char 或 varchar 的表达式必须仅包含数字和可选的加号或减号(+ 或 -)。前导空格将被忽略。转换为货币的数据类型 char 或 varchar 的表达式还可以包括可选的小数点和前导美元符号 ($)。

回答by Alec.

After a little messing around, I've figured since this happens no matter what currency symbol is used, SQL server is implying that the field is a currency field.

经过一番折腾之后,我发现无论使用什么货币符号都会发生这种情况,SQL Server 暗示该字段是货币字段。

If you add numbers after the currency symbol, in this case a dollar so:

如果在货币符号后添加数字,在本例中为美元,则:

SELECT 

SQL server will return 4.00

SQL 服务器将返回 4.00

So SQL Server is taking the use of $ and assuming we want to create a field with the MONEYdatatype and as we haven't entered a value after the currency symbol, SQL Server assumes the value is 0, though in my opinion this should return NULL.

所以 SQL Server 正在使用 $ 并假设我们要创建一个具有MONEY数据类型的字段,并且由于我们没有在货币符号后输入值,SQL Server 假定该值为 0,但在我看来这应该返回空值。

回答by Bacon Bits

You can prove out that SQL Server is treating it as a moneytype:

您可以证明 SQL Server 将其视为一种money类型:

select $ as n into #x

exec tempdb..sp_help '#x'

sp_helpwill output (among other things):

sp_help将输出(除其他外):

 Column_name     Type     Computed     Length     Prec     Scale     Nullable     TrimTrailingBlanks     FixedLenNullInSource     Collation    
 --------------  -------  -----------  ---------  -------  --------  -----------  ---------------------  -----------------------  ------------ 
 n               money    no           8          19       4         no           (n/a)                  (n/a)                    (null)