如何截断sybase ASE中的数字?

时间:2013-05-24 15:01:18

标签: sql sybase-ase

http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc38151.1540/doc/html/san1278453173757.html

Adaptive Server Enterprise不支持TRUNCATE和TRUNCNUM函数。

有没有人知道在ASE中这样做的另一种方式?

由于

3 个答案:

答案 0 :(得分:1)

我知道这些方法:

select Number = floor ( 455.443 )
select Number = cast ( 455.443 as int )
select Number = convert ( int, 455.443 )
select Number = 455.443 - ( 455.443 % 1 )

答案 1 :(得分:0)

如何使用Floor功能?它基本上做同样的事情,并在ASE中得到支持。

答案 2 :(得分:0)

这是一个古老的问题,但我几天前做过这样的事情来模仿" truncnum"上面链接中描述的功能。

create function custom_truncnum(@numberToTruncate float, @decimalPlaces int)
returns float
AS
    declare @tenToTheXPower float;
    declare @leftSideOfDecimal float;
    declare @returnVal float;

    set @tenToTheXPower = power(10, ABS(@decimalPlaces);
    set @leftSideOfDecimal = FLOOR(@numberToTruncate);

    if (@decimalPlaces <= 0)
        set @returnVal = FLOOR(@numberToTruncate / @tenToTheXPower) * @tenToTheXPower;
    else
        set @returnVal = @leftSideOfDecimal + (FLOOR(@numberToTruncate - @leftSideOfDecimal) * @tenToTheXPower) / @tenToTheXPower);

    return @returnVal;
GO

现在,您应该可以在Sybase ASE中执行此操作:

SELECT dbo.custom_truncnum(345.567, 2) 
    345.56

SELECT dbo.custom_truncnum(345.562, 2) 
    345.56

SELECT dbo.custom_truncnum(345.567, -1) 
    340

SELECT dbo.custom_truncnum(345.567, -2) 
    300

SELECT dbo.custom_truncnum(345.567, 0)  --This is the same as FLOOR(345.567)
    345