Laravel Integer to Float or Decimal MySQL Column - mysql

I have a current db table called scores that has 2 columns user_score and approved_score which are both Integer.
After initial launch we have decided to make things more exact by add a decimal to a score. So instead of getting a 10 you could get a 10.40 or a 10.47 ect. I am just curious on:
Should I change it to a decimal or a float based on us needing 2
decimal points with trailing 0. So: $table-> decimal('user_score', 8,
2)->change();
Will that mess up data in there already? Like will a
score of 123 stay 123? We rank based on score so the higher the
better.

A float would be more appropriate in this case, it doesn't sound like you need a high number of precision points after the decimal place. The data should not be impacted by changing the column type, so long as your values are not currently larger than (8,2).
Also, updating your existing data if you kept the column as an integer would be trivial as all you would need to do is UPDATE column SET column=column*100.

Related

How do I replace values in a column in KNIME?

I have a column of countries with 50 different values that I want to reduce to United States and Other.
Can someone help me with that?
Another example is Age which has 48 values that I'd like to reduce to only 4 like 1 to 18 = youth, 18-27 = starting, etc.
I've actually got about 5 columns that I want to reduce the values of. So would I need to repeat the process multiple times in KNIME or can I accomplish multiple column value replacements at once?
The latter on can easily be achieved with the Rule Engine
$Col0$ > 1 AND $Col0$ <18 => "youth"
For the First problem I'd use a String Replace (Dictionary).
I don't think you replace all at once but you can loop over columns.
For the second case I would use Numeric Binner:
For each column a number of intervals - known as bins - can be
defined. Each of these bins is given a unique name (for this column),
a defined range, and open or closed interval borders. They
automatically ensure that the ranges are defined in descending order
and that interval borders are consistent. In addition, each column is
either replaced with the binned, string-type column, or a new binned,
string-type column is appended.

How to prevent to store also the 0 in decimal?

I need to save the avg of a value, that in this case corresponds to the average attendance of a venue, and for this, I created a new field called avg which has this structure: decimal(10,3).
Now if I store a value like this: 2671, the field will contain: 2.671 that's correct, but if I need to store only 800 I'll get: 800.000 that's, of course, is a different number…
So how can I store the correct decimal value? In this case should be only 800, not 800.000.
Also, how can I adjust the bad values stored in the database without repeat all the insert?
Thanks.
The decimal point is irrelevant, you can trim the decimal point using another function.

working with floats mysql

i am customizing a Word Press plugin named Gravity Forms, now the plugin is using a column as datatype float
now i created a totally new interface for displaying the details of an entry that is submitted, i have a set of checkboxes in the form, what Gravity Forms is doing it is adding the field_number as a floats and then the value against it
now for one set of checboxes on my form it is using the field_number as 2 now the 2 remains constants no matter how many checkboxes are selected, and after that comes some points values e.g 2.1 for a specific value and then 2.2 for a specific value and so on upto n times depending on the numbers of checkboxes in the form. please see the below image for more clarification!
Important Note! i cant change the datatype of float as something else e.g vahrchar or decimal it totally messes up the plugin
now i did struggled with getting the float values, because float are not that reliable and easy to use i have seen other blogs where people prefer double or decimal over it
my main problem was this query,
SELECT value FROM wp_rg_lead_detail WHERE lead_id=".absint( $lead['id'] )." and field_number=2.4
now running this query i did not get any result, so what i did is modified my query and passed the value as decimal!
SELECT value FROM wp_rg_lead_detail WHERE lead_id=".absint( $lead['id'] )." and CAST(field_number AS DECIMAL) = CAST(2 AS DECIMAL)
now this query worked just fine and returned the number of rows, but the problem is it is missing some data and not returning all data. like if i have 5 rows as you can see the in the image above its returns only three rows and skips the two rows! any help?
If you want compare with 2 could be you need a truncate instead of a cast eg:
SELECT value
FROM wp_rg_lead_detail
WHERE lead_id=".absint( $lead['id'] )."
and truncate(field_number,0) = 2

Mysql datatype for money

i was trying to create a money related app in which users can choose their currency. Mysql datatype i tried is decimal(19,4). Now the problem is few currencies need three precisions and some need two
Eg:
oman rial needs three precisions. ie 1000 baisa = 1 omani rial. Hence my customers may enter 6.783 omani rial.
Where as my US customers will need only 2 precisions as 100 cents = 1 dollar and they may enter 5.50.
When i insert these two entries to my database using decimal(19,4), it is saved as 6.7830 and 5.5000 respectively.
Now the real pain is when i need to display their entrys as i dont want to display that extra 0 in omani rial entry and that 00 in US dollar. I also tried float but last digit gets rounded off at times.
Is there any mysql data type in which i can save exact entry as it is without any rounding off or extra zeros? If there is no such entry, how can i make it ppssible?
You can use VARCHAR to store exact representations, but I don't recommend that because it takes more bytes to store a number as a string. And any arithmetic you do on the value will convert it to a number anyway.
I recommend you use DECIMAL(19,4), and then format the value in application code, to display it with the appropriate digits. Every programming language has some function like printf() that allows you to control the output formatting, regardless of the value stored.

What datatype should I use for my Tickets table?

I'm creating a table which is set to list out the prices of tickets for specific venues of a tour in my database.
The price needs to be in UK currency (£0.00) as the tours are based in the UK. However when it comes to setting the datatypes for the price columns I was unsure what datatype would allow the format of UK currency to fit. So I used DOUBLE PRECISION but when trying to input these values:
INSERT INTO `tbl_Tickets` (`Student_Price`, `Adult_Price`, `OAP_Price`, `Tourdate_id`)
VALUES ('£15.00', '£22.00' ,'£13.50', 1);
I get a 0, each of the price columns.
What datatype should I set my price columns to, to allow UK currency to properly fit?
You should probably use a fixed-point numeric type to store currency of any nationality, eg. DECIMAL(5, 2) which would store values up to 999.99.
As spencer7593 points out below, there's no need to be stingy with the number of digits, as the DECIMAL type's storage format is quite compact.
You must never, never, never use a floating-point datatype for money. You must always use a decimal radix, in this case DECIMAL(N, 2), where N is some reasonably high upper bound on values, their sums, etc, such as ten, or much more if you're running a bank. The 2 is because you have 100 pence to the pound, but again there are cases where you might want a bit more.