SQL - How do I get only the numbers after the decimal?

后端 未结 12 2027
清酒与你
清酒与你 2020-12-03 00:57

How do I get only the numbers after the decimal?

Example: 2.938 = 938

相关标签:
12条回答
  • 2020-12-03 01:12

    The usual hack (which varies a bit in syntax) is

    x - floor(x)
    

    That's the fractional part. To make into an integer, scale it.

    (x - floor(x)) * 1000
    
    0 讨论(0)
  • 2020-12-03 01:14

    Make it very simple by query:

    select substr('123.123',instr('123.123','.')+1, length('123.123')) from dual;
    

    Put your number or column name instead 123.122

    0 讨论(0)
  • 2020-12-03 01:14

    If you want to select only decimal numbers use this WHERE clause:

        (CAST(RIGHT(Myfield, LEN( Myfield)-CHARINDEX('.',Myfield)+1 ) AS FLOAT)) <> 0
    

    If you want a clear list you can sort by decimal/integer:

        CASE WHEN 0 = CAST(RIGHT(Myfield, LEN( Myfield)-CHARINDEX('.',Myfield)+1 ) AS FLOAT) THEN 'Integer' ELSE 'Decimal' END AS Type
    
    0 讨论(0)
  • 2020-12-03 01:15

    You can use FLOOR:

    select x, ABS(x) - FLOOR(ABS(x))
    from (
        select 2.938 as x
    ) a
    

    Output:

    x                                       
    -------- ----------
    2.938    0.938
    

    Or you can use SUBSTRING:

    select x, SUBSTRING(cast(x as varchar(max)), charindex(cast(x as varchar(max)), '.') + 3, len(cast(x as varchar(max))))
    from (
        select 2.938 as x
    ) a
    
    0 讨论(0)
  • 2020-12-03 01:20

    one way, works also for negative values

    declare @1 decimal(4,3)
    select @1 = 2.938
    
    select PARSENAME(@1,1)
    
    0 讨论(0)
  • 2020-12-03 01:25

    try this:

    SELECT (num % 1)
    
    0 讨论(0)
提交回复
热议问题