Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

sql - Get the number of digits after the decimal point of a float (with or without decimal part)

I have following list of Amount (float) in my table.

Amount
123
123.1
123.0123
123.789456

How can i get the number of digits after the decimal point.

Duplicate ?: I have checked already existing posts, but there is no correct way to handle the float numbers with or without decimal part.

Result

Amount      Result
123         0
123.1       1
123.0123    4
123.789456  6

EDIT : After spending some valuable time, i have found some relatively simple script to handle this. My answer is below

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do It in following:

QUERY

SELECT Amount, 
       CASE WHEN FLOOR(Amount) <> CEILING(Amount) THEN LEN(CONVERT(INT,CONVERT(FLOAT,REVERSE(CONVERT(VARCHAR(50), Amount, 128))))) ELSE 0 END AS Result
FROM YourTable

OUPUT

Amount      Result
123         0
123,1       1
123,0123    4
123,789456  6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...