Question of the month March 2016-Why does SELECT $ return 0?

Run the following code

select $ as value

The result is

value
———————
0.00

The question is “Why does it return 0?”

 

Advertisements

3 thoughts on “Question of the month March 2016-Why does SELECT $ return 0?

  1. As per this MSDN link – https://msdn.microsoft.com/en-us/library/ms179882.aspx – $ sign is used as or indicate money datatype in sql server. SQL Server recognize different currency simbol given in provided MSDN link. So if you write “Select $11 as value” gives 11.00 in result and “Select $0 as value” query gives you 0.00 in result. So $ and $0 in select statement are same. That means – “Select $ as value1, $0 as value2” gives you the same result.

  2. Krishnraj, this is great description of the behavior, but not the reason 🙂

    The reason is that SQL Server engine uses explicit convert behind the scene, and this is the rule for implicit convert of empty string to type money.

    As Krishnraj wrote, by using the sign $, SQL Server recognizes the type as money. The reason that $ converted to 0.00 is that the rule for implicit convert of empty string to type money is to convert it to 0. Using $ without a value before is just the same like using empty string.

    By the way, the same rule applied to any other numeric which is allowing converting empty string directly. you can check this query for example (all return 0 value):
    select
    CONVERT(money ,$)
    ,CONVERT(money ,’$’)
    ,CONVERT(money ,”)
    ,CONVERT(bigint ,”)
    ,CONVERT(bit ,”)
    –,CONVERT(decimal ,”) — Error
    ,CONVERT(int ,”)
    –,CONVERT(numeric ,”) — Error
    ,CONVERT(smallint ,”)
    ,CONVERT(smallmoney,”)
    ,CONVERT(tinyint ,”)
    ,CONVERT(float ,”)
    ,CONVERT(real ,”)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s