Share
Go down
avatar
Admin
Posts : 155
Join date : 2017-11-11
Age : 27
View user profilehttp://net-media.yoo7.com

how to convert int to decimal in SQL

on Wed Dec 13, 2017 11:02 am
I am working on an to solve a problem that I have a textbox that accepts any type of data but I want to cast the data to decimal(12,9) or any data type can accept this number as an example 42.56926437219384

here is the problem the user can enter any kind of data like characters or integer

1st case I want to handle if the data entered as integer:
Code:
DECLARE @num DECIMAL(12,9) = 444444444444444
SELECT CONVERT(DECIMAL(12,9),@num)
I think if it is characters will handle it in the solution and assign validation for the textbox.

how can I handle the integer part?

Answer
When you specify it as DECIMAL(12,9) this means your number can have up to 12 digits (excluding . ) and out of that 9 digits will be after decimal part. Which means the maximum value that you can store here is 999.999999999.

in your sample scenario above, the number has got 15 integer parts which are too high than the scope of your variable, as you can do either of the following

Change the size of variable from DECIMAL(12,9) to higher integer precision like DECIMAL(25,9)
Add a validation on your application to restrict the number of characters that the user can enter
Back to top
Permissions in this forum:
You can reply to topics in this forum