如果我要写一个分数的sql语句。
如 select 1/3。怎么显示0
如何可以显示小数点。
除了人为处理为 select 1.00/3.00

解决方案 »

  1.   

    select 1.0/3
    /*
              
    --------- 
    .333333(所影响的行数为 1 行)
    */select cast(1.0/3 as decimal(18,2))
    /*
                         
    -------------------- 
    .33(所影响的行数为 1 行)
    */
      

  2.   

    你首先要理解 / 這個的意思,
    看sql online bookResult Types
    Returns the data type of the argument with the higher precedence. For more information about data type precedence, see Data Type Precedence. If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.

    Res
    The actual value returned by the / operator is the quotient of the first expression divided by the second expression.
      

  3.   

    数据类型问题,你可以看看:
    select cast(1 as dec(8,2))/3
      

  4.   

    附上,Data Type Precedencesql_variant (highest)
    datetime
    smalldatetime
    float
    real
    decimal
    money
    smallmoney
    bigint
    int
    smallint
    tinyint
    bit
    ntext 
    text
    image
    timestamp
    uniqueidentifier 
    nvarchar
    nchar
    varchar
    char
    varbinary
    binary (lowest) 
      

  5.   


    select 1.0/3
    select cast(1.0/3 as decimal(18,2))