stripping decimal data from an nvarchar value...

I have a table that contains an nvarchar column of data. The data is actually a monetary value; sometimes with a decimal point sometimes without.

My problem is that I need to strip the decimal portion of the string if it exists. From a select statement I can use:

SUBSTRING(DW_OBP_ORD_TMP_IC.VALUE,1,LEN(VALUE)-(LEN(VALUE)-CHARINDEX('.',DW_OBP_ORD_TMP_IC.VALUE)

if a decimal point exists. But if one does not the CharIndex comes back 0 and my equation does not work correctly.

I need to pull the data from the table, along with many other fields. How do I do this without using a cursor I have millions of rows so need a solution that will be quick.

thanks in advance,

Marilyn



Answer this question

stripping decimal data from an nvarchar value...

  • TA123

    Marilyn:

    If you ever have to worry about whether or not the number is formatted correctly:

    set nocount on

    declare @mockUp table (aValue nvarchar (20) not null)
    insert into @mockUp values ('+2E5')
    insert into @mockUp values ('-1.45001D2')
    insert into @mockUp values ('45')
    insert into @mockUp values ('127.14')
    insert into @mockup values ('+61.47')
    insert into @mockup values ('-461.')
    insert into @mockup values ('Bad Value')

    -- ---------------------------------------------------------
    -- The advantage of the case statement is that if the
    -- data is an invalid value that it does not cause an
    -- execution error.
    --
    -- Try un-commenting either of the converts and you will
    -- get an execution error similar to this:
    --
    --
    -- Server: Msg 8114, Level 16, State 5, Line 21
    -- Error converting data type nvarchar to float.
    --
    --
    -- The case statement makes the conversion more bullet
    -- proof.
    -- ---------------------------------------------------------
    select aValue,
    isNumeric (aValue) as [isNumeric],
    -- convert (float, aValue) [Float Value],
    -- convert (int, convert(float, aValue) ) [Int Value],
    case when isNumeric (aValue) = 1
    then convert (int, convert(float, aValue))
    else null
    end as convertedValue
    from @mockup

    -- ---------------------------------------------
    -- Sample Output:
    -- ---------------------------------------------

    -- aValue isNumeric convertedValue
    -- -------------------- ----------- --------------
    -- +2E5 1 200000
    -- -1.45001D2 1 -145
    -- 45 1 45
    -- 127.14 1 127
    -- +61.47 1 61
    -- -461. 1 -461
    -- Bad Value 0 NULL


  • GJVH

    select cast(cast(value as float)as int) from tablename



  • donkaiser

    Nitin Khurana wrote:

    select cast(cast(value as float)as int) from tablename



    That will work as well.


  • KevinRevenueResearch

    Convert the field to numeric, then convert to int. That is if you can ensure that all of the data is monetary.

    Examples:
    select '45.64' as col1, cast(cast('45.64' as numeric(4,2)) as int) as col2
    select '34' as col1, cast(cast('34' as numeric(4,2)) as int) as col2


  • stripping decimal data from an nvarchar value...