How do I count decimal places in SQL?

前端 未结 6 721
日久生厌
日久生厌 2020-12-06 01:30

I have a column X which is full of floats with decimals places ranging from 0 (no decimals) to 6 (maximum). I can count on the fact that there are no floats with greater th

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 02:08

    This thread is also using CAST, but I found the answer interesting:

    http://www.sqlservercentral.com/Forums/Topic314390-8-1.aspx

    DECLARE @Places INT
     SELECT TOP 1000000 @Places = FLOOR(LOG10(REVERSE(ABS(SomeNumber)+1)))+1
       FROM dbo.BigTest
    

    and in ORACLE:

    SELECT FLOOR(LOG(10,REVERSE(CAST(ABS(.56544)+1 as varchar(50))))) + 1 from DUAL
    

提交回复
热议问题