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

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

How do I get only the numbers after the decimal?

Example: 2.938 = 938

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

    You can use RIGHT :

     select RIGHT(123.45,2) return => 45
    
    0 讨论(0)
  • 2020-12-03 01:00

    If you know that you want the values to the thousandths, place, it's

    SELECT (num - FLOOR(num)) * 1000 FROM table...;
    
    0 讨论(0)
  • 2020-12-03 01:01

    I had the same problem and solved with '%' operator:

    select 12.54 % 1;
    
    0 讨论(0)
  • 2020-12-03 01:02

    More generalized approach may be to merge PARSENAME and % operator. (as answered in two of the answers above)

    Results as per 1st approach above by SQLMenace

    select PARSENAME(0.001,1) 
    

    Result: 001

    select PARSENAME(0.0010,1) 
    

    Result: 0010

    select PARSENAME(-0.001,1)
    

    Result: 001

    select PARSENAME(-1,1)
    

    Result: -1 --> Should not return integer part

    select PARSENAME(0,1)
    

    Result: 0

    select PARSENAME(1,1)
    

    Result: 1 --> Should not return integer part

    select PARSENAME(100.00,1)
    

    Result: 00

    Results as per 1st approach above by Pavel Morshenyuk "0." is part of result in this case.

    SELECT (100.0001 % 1)
    

    Result: 0.0001

    SELECT (100.0010 % 1)
    

    Result: 0.0010

    SELECT (0.0001 % 1)
    

    Result: 0.0001

    SELECT (0001 % 1)
    

    Result: 0

    SELECT (1 % 1)
    

    Result: 0

    SELECT (100 % 1)
    

    Result: 0

    Combining both:

    SELECT PARSENAME((100.0001 % 1),1)
    

    Result: 0001

    SELECT PARSENAME((100.0010 % 1),1)
    

    Result: 0010

    SELECT PARSENAME((0.0001 % 1),1)
    

    Result: 0001

    SELECT PARSENAME((0001 % 1),1)
    

    Result: 0

    SELECT PARSENAME((1 % 1),1)
    

    Result: 0

    SELECT PARSENAME((100 % 1),1)
    

    Result: 0

    But still one issue which remains is the zero after the non zero numbers are part of the result (Example: 0.0010 -> 0010). May be one have to apply some other logic to remove that.

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

    X - TRUNC(X), works for negatives too.

    It would give you the decimal part of the number, as a double, not an integer.

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

    CAST(RIGHT(MyField, LEN( MyField)-CHARINDEX('.',MyField)+1 ) AS FLOAT)

    0 讨论(0)
提交回复
热议问题