Question

exist any way to store in database a numbers with or without decimal point? The user can store to database numbers like 10 and 10.1 - for storing numbers in MySQL I use data type column decimal, so for this example, in the database will be stored 10.00 and 10.10.

But I will need to store the exact form of number by user - so in this case 0 10 and 10.1.

Is possible to solve it on database level or I have to edit on application level?

Was it helpful?

Solution

make use on case

case format(@number,0)=@number 
  when 1 then format(@number,0) 
  else 
  case format(@number,1)=@number 
    when 1 then format(@number,1) 
    else @number
  end 
end

or if

if (format(@number,0)=@number, format(@number,0),
  if (format(@number,1)=@number, format(@number,1), @number))

OTHER TIPS

one way is when you make the select query to omit this leading zero

http://www.foscode.com/remove-trailing-zeros-mysql-decimal-fields/

You could just save whatever the user entered as a VARCHAR, so you'll store exactly what they entered. Then when you need it, you can load it from the DB, and parse it to the corresponding float value as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top