VARCHAR to DECIMAL

前端 未结 12 658
清歌不尽
清歌不尽 2020-12-06 09:24

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

When I try to use cast or convert I am getting an arit

12条回答
  •  情歌与酒
    2020-12-06 09:42

    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
    

提交回复
热议问题