sql

select 按小数位取值=left(数值,len(数值)-4+小数位数)
from 表

解决方案 »

  1.   

    --示例--示例数据
    declare @t table(ID int,数值 decimal(10,4),小数位数 int)
    insert @t select 1,1.0000,2
    union all select 2,2.0000,3  
    union all select 3,3.0000,4--查询
    select *,按小数位取值=left(数值,len(数值)-4+小数位数)
    from @t/*--测试结果ID          数值           小数位数    按小数位取值       
    ----------- ------------ ----------- -------------------
    1           1.0000       2           1.00
    2           2.0000       3           2.000
    3           3.0000       4           3.0000(所影响的行数为 3 行)
    --*/
      

  2.   

    select left(cast(数值 as varchar(100)),charindex('.',cast(数值 as varchar(100)))+小数位数) as 数值 from 表
      

  3.   

    create table TB(ID_1 int,數值 numeric(18,4),小數位數 int)
    insert into TB  select 1,1.0000,2
    union select 2,2.0000,3
    union select 3,3.0000,4
    select 數值 =left(數值,len(數值)-4+小數位數)
    from tb
      

  4.   

    select round(数值,小数位数)
      

  5.   

    STR(数值, LEN(数值), 小数)
      

  6.   

    select 數值=round(數值,小数位数) from 表
      

  7.   

    把zjcxc(邹建)的查询改了一下
    create   table t(ID int,数值 decimal(10,4),小数位数 int)
    insert t select 1,1.0000,2
    union all select 2,2.0000,3  
    union all select 3,3.0000,4
    --查询
    SELECT *,按小数位取值=LEFT(CAST(数值 AS NVARCHAR(100)),LEN(CAST(数值 AS NVARCHAR(100)))-((SELECT MAX(小数位数) FROM T)-小数位数)) FROM T
    drop table t
      

  8.   

    zjcxc(邹建)大哥,还能不能根据四舍五入来决定小数的位数
      

  9.   

    select 數值=round(數值,小数位数) from 表
    上面的不行改為﹕
    select 數值=left(數值,len(數值)-4+小数位数)
    from 表
      

  10.   

    select *,cast(cast(数值 as int) as varchar)+'.'+left('0000',小数位数) from test
    1 1.0000 2 1.00
    2 2.0000 3 2.000
    3 3.0000 4 3.0000
      

  11.   

    根据四舍五入来决定小数的位数﹕
    select 數值=left(round(數值,小数位数),len(數值)-4+小数位数)
    from 表
      

  12.   

    搞定了,谢谢各位!
    恭喜zjcxc(邹建)大哥 MVP!
      

  13.   

    根据round是有问题的.--下面的示例说明这个问题
    declare @t table(a float,b int,c decimal(10,4),d int)
    insert @t select 12.2131293,3,12.2342,2select float类型=round(a,b),decimal类型=round(c,d)
    from @t--结果:float类型                                               decimal类型    
    ----------------------------------------------------- ------------ 
    12.212999999999999                                    12.2300(所影响的行数为 1 行)
      

  14.   

    如果你是要实现四舍五入的效果,则是用我的方法+round(对decimal类型)left(round(数值,小数位数),len(数值)-4+小数位数)