precision and scale in decimal(p,s)

  • I am having trouble setting precision and scale dynamically in a function:

    SET @RETURNIT = CAST(0.12121 AS DECIMAL(@P,@S))

    where @P and @S are declared as int and set with values.

    Is this not possible????

  • I think the only way to do this is to build it dynamically in a variable and then execute the SQL in the variable. Something like this:

    DECLARE @p int, @S int, @RETURNIT decimal, @strSQL varchar(300)

    SET @p = 8

    SET @S = 6

    SET @strSQL = 'DECLARE @RETURNIT decimal(' + LTRIM(STR(@P)) +',' + LTRIM(STR(@S)) + ')

    SET @RETURNIT = CAST(0.12121 AS DECIMAL(' + LTRIM(STR(@P)) +',' + LTRIM(STR(@S)) + '))

    SELECT @RETURNIT'

    PRINT @strSQL

    EXEC (@strSQL)

    Robert Marda

    SQL Server will deliver its data any way you want it

    when you give your SQL Programmer enough developing time.

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply