当前位置: 代码迷 >> Sql Server >> 此函数如何写
  详细解决方案

此函数如何写

热度:11   发布时间:2016-04-24 10:04:44.0
此函数怎么写
自定义函数
小数点后保留一位;末位保留到整数5元或0元;建议按照1-2元的舍弃,3-4元进位为5元,6-7元为5元,8-9元为10元。
------解决方案--------------------
自己都把思路分析的这么清晰了还不会写?
------解决方案--------------------

create  function myfunc(@p int)
returns int
begin
declare @m int ;
set @m = @p / 10 * 10 ;

set @p = @p - @m;
select @p= case when @p in (0,1,2) then 0 
                when @p in (3,4,5,6,7) then 5
                when @p in (8,9) then 10 
           end ;
return @m + @p ;            
end
go
select  distinct top 30 number , dbo.myfunc(number)  result
from master..spt_values 
where number > 0 order by 1 
go
number      result
----------- -----------
1           0
2           0
3           5
4           5
5           5
6           5
7           5
8           10
9           10
10          10
11          10
12          10
13          15
14          15
15          15
16          15
17          15
18          20
19          20
20          20
21          20
22          20
23          25
24          25
25          25
26          25
27          25
28          30
29          30
30          30

(30 行受影响)



------解决方案--------------------
就是一段CSE WHEN 没必要写函数 效率不高
  相关解决方案