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 3963196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:11:11+00:00 2026-05-20T03:11:11+00:00

I have some dirty input data that is being imported into a raw source

  • 0

I have some dirty input data that is being imported into a raw source table within SQL Server (2008 R2). Fields that are defined as decimal(9,2) or decimal(4,2) by the input provider are coming in as strings, however, the strings do not always conform to the data definition (go figure!).

We import the data from flat files into the raw tables,then apply some conversion scripts to insert the ‘cleaned’ data into tables with the proper data types assigned to columns.

For instance:

raw_table
TotalAmount varchar(12)

clean_table
TotalAmount decimal(9,2)

Now, my question is this. If I want to do some ‘basic’ cleanup on this, I would want to do it in a function along the lines of:

CREATE FUNCTION [dbo].[StringToDecimal]
(
    @conversionString VARCHAR(12)
)   
RETURNS DECIMAL(9,2)
AS
BEGIN   

    DECLARE @rsp DECIMAL(9,2)

    IF ISNUMERIC( LTRIM(RTRIM(REPLACE(@conversionString,' ',''))) ) = 1
         BEGIN
             SET @rsp = ISNULL( CONVERT( decimal(17,6), NULLIF( LTRIM(RTRIM(REPLACE(@conversionString,' ',''))),'') ), 0 )
         END
    ELSE
         BEGIN
             SET @rsp = 0 -- or we can return NULL here
         END

    RETURN @rsp
END

However, how could one go about supporting various sized decimals in this mix? Is there a way to parametrize the response type? I considered just returning a decimal of the largest size we generally see, then converting it again on the other end, however, you run into arithmetic overflow issues.

Would appreciate any thoughts/insight into solving this one!

  • 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-05-20T03:11:12+00:00Added an answer on May 20, 2026 at 3:11 am

    Is there a way to parametrize the response type?

    It’s simpler than you think. Just return as a VARCHAR and do the casting to decimal(x,y) from the VARCHAR. You don’t even need to cast – you can directly assign a VARCHAR (as long as it holds valid decimal data) to a decimal column/variable.

    I will create 2 functions instead. StringToDecimal2 does the actual conversion, but returns one of 6 “error codes”. You can use it to check why a string is invalid. Or use the wrapper dbo.StringToDecimal which just turns the invalid codes into NULL.

    CREATE FUNCTION [dbo].[StringToDecimal2]
    (
        @conversionString VARCHAR(12),
        @precision int,  -- total digits
        @scale int  -- after decimal point
    )   
    RETURNS VARCHAR(100)
    AS
    BEGIN
        -- remove spaces, we'll allow this error. no need to trim
        set @conversionString = REPLACE(@conversionString,' ','')
        -- note: 1,234.56 (thousands separated) will be invalid, so will 1,234,56 (European decimals)
        -- well, ok, let's clean up the thousands separators. BUT! It will incorrectly scale European decimals
        set @conversionString = REPLACE(@conversionString,',','')
    
        -- we don't support scientific notation either, so 1e4 (10,000) is out
    
        if @conversionString like '%[^0-9.+-]%' return 'INVALID1' -- only digits and decimal are valid (plus +-)
        if @conversionString like '%.%.%' return 'INVALID2' -- too many decimals
        if @conversionString like '_%[+-]%' return 'INVALID3' -- +- symbol not in the first position
        if @conversionString like '[.+-]' return 'INVALID4' -- a single character from "+-."
        if @conversionString like '[+-].' return 'INVALID5' -- symbol and decimal only
    
        -- add a decimal place so it is easier to work with below
        if @conversionString not like '%.%'
            set @conversionString = @conversionString + '.'
    
        -- allow decimal places to go only as far as scale
        set @conversionString = left(@conversionString, charindex('.', @conversionString)+@scale)
    
        -- ensure the data is within precision number of digits in total
        if charindex('.', @conversionString) > @precision - @scale + 1
            return 'INVALID6' -- too many digits before decimal
    
        RETURN @conversionString
    END
    GO
    
    CREATE FUNCTION [dbo].[StringToDecimal]
    (
        @conversionString VARCHAR(12),
        @precision int,  -- total digits
        @scale int  -- after decimal point
    )
    RETURNS VARCHAR(100)
    AS
    BEGIN
    RETURN case when [dbo].[StringToDecimal2](@conversionString, @precision, @scale) like 'INVALID%'
    then null else [dbo].[StringToDecimal2](@conversionString, @precision, @scale) end
    END
    GO
    

    Some tests:

    select [dbo].[StringToDecimal2]('12342342', 9,2)
    
    select convert(decimal(9,2),[dbo].[StringToDecimal]('1234234', 9,2))
    select convert(decimal(9,2),[dbo].[StringToDecimal]('12342342', 9,2))
    select convert(decimal(9,2),[dbo].[StringToDecimal]('123423.3333', 9,2))
    select convert(decimal(20,10),[dbo].[StringToDecimal]('123423sd.3333', 20,10))
    select convert(decimal(20,10),[dbo].[StringToDecimal]('123423sd..3333', 20,10))
    select convert(decimal(20,10),[dbo].[StringToDecimal]('-123423.3333', 20,10))
    select convert(decimal(20,10),[dbo].[StringToDecimal]('+123423..3333', 20,10))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some input data that sometimes appears with &nbsp characters on the end.
I use Entity Framework to access my SQL data. I have some constraints in
I have a script that appends some rows to a table. One of the
I have some UI in VB 2005 that looks great in XP Style, but
I have some kind of test data and want to create a unit test
We have some files on our website that users of our software can download.
I have a JLabel that needs to display some html-formatted text. However, I want
I have an ASP.NET page that has three div s within the only form
I have some code here to call minizip(), a boilerplate dirty renamed main() of
I have some html that creates a dropdown list. The list has text values

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.