Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Arithmetic overflow error converting expression to data type nvarchar.

Arithmetic overflow error converting expression to data type nvarchar.

Scheduled Pinned Locked Moved Database
csharpasp-netcomhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vimalsoft Pty Ltd
    wrote on last edited by
    #1

    Good Day i have been hit by this error this night. I have a UDF defined like this

    CREATE FUNCTION [dbo].[funcFormatPercentages_Extended]
    (

    -- Add the parameters for the function here

    @parPercentageToBeFormatted nvarchar(20)

    )
    RETURNS nvarchar(20)

    AS

    BEGIN
    RETURN CAST(CAST(convert(float,@parPercentageToBeFormatted)* 100 AS decimal(4, 1)) AS nvarchar(5)) + N'%'
    END

    so i will call the UDF with the value like this

    select [dbo].[funcFormatPercentages_Extended_numeric]('-43.4703076923077')

    OR

    select [dbo].[funcFormatPercentages_Extended_numeric]('36.403813624481')

    i get an Error

    Msg 8115, Level 16, State 2, Line 1
    Arithmetic overflow error converting expression to data type nvarchar.

    I tried to change the datatypes and Precision around , but still the error. Thanks

    Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

    M Richard DeemingR 2 Replies Last reply
    0
    • V Vimalsoft Pty Ltd

      Good Day i have been hit by this error this night. I have a UDF defined like this

      CREATE FUNCTION [dbo].[funcFormatPercentages_Extended]
      (

      -- Add the parameters for the function here

      @parPercentageToBeFormatted nvarchar(20)

      )
      RETURNS nvarchar(20)

      AS

      BEGIN
      RETURN CAST(CAST(convert(float,@parPercentageToBeFormatted)* 100 AS decimal(4, 1)) AS nvarchar(5)) + N'%'
      END

      so i will call the UDF with the value like this

      select [dbo].[funcFormatPercentages_Extended_numeric]('-43.4703076923077')

      OR

      select [dbo].[funcFormatPercentages_Extended_numeric]('36.403813624481')

      i get an Error

      Msg 8115, Level 16, State 2, Line 1
      Arithmetic overflow error converting expression to data type nvarchar.

      I tried to change the datatypes and Precision around , but still the error. Thanks

      Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      A guess only, try removing the N'%' and see if it makes a difference. % is an operator and the interpretor may be making a mistake on the operation.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • V Vimalsoft Pty Ltd

        Good Day i have been hit by this error this night. I have a UDF defined like this

        CREATE FUNCTION [dbo].[funcFormatPercentages_Extended]
        (

        -- Add the parameters for the function here

        @parPercentageToBeFormatted nvarchar(20)

        )
        RETURNS nvarchar(20)

        AS

        BEGIN
        RETURN CAST(CAST(convert(float,@parPercentageToBeFormatted)* 100 AS decimal(4, 1)) AS nvarchar(5)) + N'%'
        END

        so i will call the UDF with the value like this

        select [dbo].[funcFormatPercentages_Extended_numeric]('-43.4703076923077')

        OR

        select [dbo].[funcFormatPercentages_Extended_numeric]('36.403813624481')

        i get an Error

        Msg 8115, Level 16, State 2, Line 1
        Arithmetic overflow error converting expression to data type nvarchar.

        I tried to change the datatypes and Precision around , but still the error. Thanks

        Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        decimal(4, 1) means a number with three digits before the decimal point and one digit after: http://msdn.microsoft.com/en-gb/library/ms187746.aspx[^] Based on your provided inputs: 36.403 * 100 = 3640.3 Therefore, your result requires four digits before the decimal point, so the precision should be 5:

        RETURN CAST(CAST(CONVERT(float, @parPercentageToBeFormatted) * 100 AS decimal(5, 1)) AS nvarchar(5)) + N'%'


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups