Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...