Bishoy Ezzat
Bishoy Ezzat

Reputation: 155

how to convert int to decimal in SQL

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:

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?

Upvotes: 1

Views: 11141

Answers (1)

Jayasurya Satheesh
Jayasurya Satheesh

Reputation: 8033

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

  1. Change the size of variable from DECIMAL(12,9) to higher integer precision like DECIMAL(25,9)
  2. Add a validation on your application to restrict the number of characters that the user can enter

Upvotes: 4

Related Questions