Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:21:16+00:00 2026-06-09T00:21:16+00:00

I want to convert a varchar(max) column to decimal(10,4) . When I try to

  • 0

I want to convert a varchar(max) column to decimal(10,4).

When I try to use cast or convert I am getting an arithmetic overflow exception. The issue is that the data stored in the varchar column may contain different precisions and different scales. For example, 123456789.1234567′, 1.12345678 or 123456.1234.

For values like 123456.1234 it is converting with out any issue but for other values I am having some problems.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:21:17+00:00Added an answer on June 9, 2026 at 12:21 am

    Implemented using Custom Function.
    This will check whether the string value can be converted to Decimal safely

    CREATE FUNCTION [dbo].[TryParseAsDecimal]
    (
        @Value      NVARCHAR(4000)
        ,@Precision INT
        ,@Scale     INT
    )
    
    RETURNS BIT
    AS
    BEGIN
    
        IF(ISNUMERIC(@Value) =0) BEGIN
            RETURN CAST(0 AS BIT)
        END
        SELECT @Value = REPLACE(@Value,',','') --Removes the comma
    
        --This function validates only the first part eg '1234567.8901111111'
        --It validates only the values before the '.' ie '1234567.'
        DECLARE @Index          INT
        DECLARE @Part1Length    INT 
        DECLARE @Part1          VARCHAR(4000)   
    
        SELECT @Index = CHARINDEX('.', @Value, 0)
        IF (@Index>0) BEGIN
            --If decimal places, extract the left part only and cast it to avoid leading zeros (eg.'0000000001' => '1')
            SELECT @Part1 =LEFT(@Value, @Index-1);
            SELECT @Part1=SUBSTRING(@Part1, PATINDEX('%[^0]%', @Part1+'.'), LEN(@Part1));
            SELECT @Part1Length = LEN(@Part1);
        END
        ELSE BEGIN
            SELECT @Part1 =CAST(@Value AS DECIMAL);
            SELECT @Part1Length= LEN(@Part1)
        END 
    
        IF (@Part1Length > (@Precision-@Scale)) BEGIN
            RETURN CAST(0 AS BIT)
        END
    
        RETURN CAST(1 AS BIT)
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a varchar value that iI set up to a date
I want to convert a column of numbers to numeric, but there are certain
I want only Hr and Min in time format so i did select convert(varchar(20),GETDATE(),108)
I want to cast a date into ISO format, and use it as a
select * from sampleTable where CONVERT(VARCHAR(20),DateCreated,101) = CONVERT(VARCHAR(20),CAST('Feb 15 2012 7:00:00:000PM' AS DATETIME),101) I
I have a variable that I want to return a max of 5 to
This is my query. SELECT CONVERT(varchar, cast(date as datetime), 3) FROM shoptransfer GROUP BY
I have a query select *, right( convert(varchar, cast(JOB_DONE_time-JOB_send_time as datetime), 121), 12 )
I want to select the max value of a varchar field and increment its
I want to convert a string to formatted text in C# in WPF application,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.