Scalar-valued Functions [dbo].[trunc]
Properties
PropertyValue
ANSI Nulls OnYes
Quoted Identifier OnYes
Parameters
NameData TypeMax Length (Bytes)
@xfloat8
@nint4
SQL Script
CREATE FUNCTION [dbo].[trunc](@x as float, @n as int = 0)
returns float
begin
  declare @retval float  

  if @n is null or @x is null
    begin
      set @retval = null
      return @retval
    end
  
  if @x > 0
    set @retval = floor(@x * power(cast(10 as float), @n)) / power(cast(10 as float), @n)
  else
    set @retval = ceiling(@x * power(cast(10 as float), @n)) / power(cast(10 as float), @n)

  return @retval
end
GO
Uses
Used By