id  name  price  pricea
1    001   100
2    002   200c现在数据库表products有以上字段,这个pricea的值是price*1.5/6.8,我用以下语句执行后,pricea的小数点位数不确定,
我想改成小数点后面只要取两位数的就行了.update products set pricea=price*1.5/6.8这句语句要如何修改,谢谢..

解决方案 »

  1.   

    update products set pricea=cast(price*1.5/6.8 as decimal(18,2))
      

  2.   


    update products set pricea=cast(price*1.5/6.8 as numeric(8,2)) 
      

  3.   

    update products set pricea=cast(price*1.5/6.8 decimal(18,2))
      

  4.   

    谢谢各位星星的高手,郁闷,竞然没有一个会正确执行出来.
    update products set pricea=cast(price*1.5/6.8 as decimal(18,2))
    可以执行成功,但是数据还是没有保留小数点两位,后面带了好多位.
    update products set pricea=cast(price*1.5/6.8 as numeric(8,2)) 提示:
    服务器: 消息 8115,级别 16,状态 6,行 1
    将 float 转换为数据类型 numeric 时发生算术溢出错误。
    语句已终止。update products set pricea=cast(price*1.5/6.8 decimal(18,2)) 
    提示:
    服务器: 消息 170,级别 15,状态 1,行 1
    第 1 行: 'decimal' 附近有语法错误。
      

  5.   

    对了,price,pricea都是float类型的
      

  6.   

    update products set pricea=cast((price*1.5/6.8) as decimal(18,2)) 加个括号,要不只转6.8了
      

  7.   

    再次谢谢各位的帮忙,奇怪了,还是不行..update products set pricea=cast(price*1.5/6.8 as numeric(8,2)) 
    提示: 
    服务器: 消息 8115,级别 16,状态 6,行 1
    将 float 转换为数据类型 numeric 时发生算术溢出错误。
    语句已终止。
    update products set pricea=cast((price*1.5/6.8) as decimal(18,2)) 执行成功,但还是后面有很多小数点,不是两位数.
      

  8.   

    create table tb(price float)
    insert into tb select 23425.8823update tb set price=cast((price*1.5/6.8) as decimal(18,2))select * from tb5167.47
      

  9.   

    wzy_love_sly ,谢谢您,您的代码执行过后,小数点仍是有很多位的,你那边执行通过吗?
      

  10.   

    不过,库里面存储的已经是小数点后两位了。是不是在查询时因为提取的是float类型的。所以才会出现错误!
      

  11.   

    update products set pricea=cast((price*1.5/6.8) as decimal(18,2))
      

  12.   

    --4舍5入
    update products set pricea=round(price*1.5/6.8,2)
      

  13.   

    输出的时候变成字符显示就是两位了
    select cast(price as varchar) from tb
      

  14.   

    update products set pricea=cast(price*1.5/6.8 as decimal(18,2))  http://www.hnd.cc