I need to save numbers to database as it is please see the example.
currently i save number as 22.5 and in database it is saved as 22.
number in database is set as integer
//sometimes $number can be "33.5" or "33" or "1" or "1.003"or "03"
$sql = "INSERT INTO active (name, number) VALUES ('somename','$number')";
If you make your number column as DECIMAL data type, it will hold the decimal point with all numbers. Integer columns cannot hold decimal points.
If it is not your requirement, better use VARCHAR as data type (But NOT RECOMENDED).
Hope this helps.
First of all, you need to check the structure of active table. From your question, I understand that various types of number values is going to be saved to number column. And I guess the type of number column in active table is numeric for int types. For example, INT or BIGINT or any other integer types.
Here is what I'm usually doing. Just use varchar for number field. It might not sound like professional. But it's very easy to use. By doing so, you won't have any problem with saving numeric value to the column. I have got an similar issue before while saving latitude and longitude value to the table. And php has got many functions to get actual float or int value from string. So this is an easy and simple way to fix.
One of the easiest and reliable way to save the numbers/integers/floats to the database is to convert them to string and then storing them into the database.
Note : Your database column type should be of type string/text.
One of the advantage of this method is that you can overcome the limits of int/float/double.
$string_number = (string)$number;
$float_number = (float)$string_number;
If you want to save number as 22.5 and in database. Then you need to change table column structure. Instead of Integer/Int set to float Then Next set length 10,2
for information please check below screen short
Then you get proper output Happy Programming
Related
I want to fetch the total bill amount of particular day, but in my database all the amount are inserted with "£" symbol. Now I want to sum all the column values.can anyone help me to find out
For example:
I have created a table called order and the columns are id, totamt. In totamt column data is inserted like float values eg. £2.00, £90.00 etc...
I have tried to add the values using Sum function in sql query. It adds the value but the total amount is wrong.
I guess, as others in comments have guessed, your totamt column has a VARCHAR() data type, not the FLOAT data type you mentioned in your question.
To solve this problem the best way, you should rework your database so your totamt column has a DECIMAL(12,2) data type. Why? Strings are hard to use for arithmetic operations as you have discovered. Also, floating-point arithmetic has some quirks making it not so good for money values.
In the meantime, you need to add code to your query to edit your totamt strings so they look like numbers before you give them to the SUM() operation.
This may work for you to convert your values. It removes the currency symbols and expresses the amount as a decimal value.
CONVERT(REPLACE(totamt,'£', ''), DECIMAL(12,2)) totamt
So you can then do
SELECT SUM(CONVERT(REPLACE(totamt,'£', ''), DECIMAL(12,2))) grand_total,
id
FROM table
GROUP BY id
i have a Problem using WHERE in my Select query.
The DB-table contains a field that stores data divided by a comma like "1,2,3".
I dont know how to check if the field contains 1,2 or 3. The usual query would be
"SELECT * FROM ".$table." WHERE name = '".$val."'".
But of course this only finds entries that equal $val, so if I search for "1" it will only give me the entries only containing "1", not "1,2,3" or "1,2". Is there a way to do that?
Thanks guys
You should fix your data structure. There are lots of good reasons to avoid storing lists of numbers as strings:
Values should be stored as their correct data types. Numbers are not strings.
SQL has pretty poor string processing functionality.
SQL has this great data structure for storing lists. It is called a table.
If the numbers refer to another table, then you cannot declare proper foreign key relationships.
Sometimes, we are stuck with other people's really bad design decisions. In those cases, MySQL offers find_in_set():
where find_in_set(1, name) > 0
I'm trying to find the max value contained in two separate fields of my table.
The code I'm using in my model is:
$query = $this->db->query("select max($field1) as max_id_1 from default_table1");
$row1 = $query->row_array();
$query = $this->db->query("select max($field2) as max_id_2 from default_table1");
$row2 = $query->row_array();
return max($row1['max_id_1'], $row2['max_id_2']);
I'm a complete novice where PHP and CodeIgniter is concerned - as I'm sure my code demonstrates :)
It is working insofar as it's returning values, but not the maximum values I have in the fields. For instance I know there is a 4000 value but the highest returned is 750.
I'm wondering if this is because the fields are of type VARCHAR because although they predominantly contain numbers there are some that contain characters (- or &) or the word 'to' so I couldn't use the INT type.
Because of using VARCHAR is it failing to see that 4000 is larger than 750?
If so is there a way to cast the field contents as integer before checking for the max value, and will this be affected by the non-integer values in the fields?
All offers of help and advice is gratefully received.
Tony.
This can be done using SQL using MySQL's implicit type conversion:
select max(case when (field1+0)>(field2+0) then field1+0 else field2+0 end)
from default_table1
Using +0 would convert varchar to number and also ignore any characters that follow after the number. If you still need the original content, you can write the query like this:
select case when (field1+0)>(field2+0) then field1 else field2 end
from default_table1
order by case when (field1+0)>(field2+0) then field1+0 else field2+0 end desc
limit 1
Oh, oops, I've forgot the mysql part
You should do the same thing, cast to integer:
select max(cast($field1 to unsigned)) as max_id_1 from default_table1
It depends of your data, but you may try something like that
return max((int)$row1['max_id_1'], (int)$row2['max_id_2']);
Have a look to the PHP doc on string to int conversion
What you are really asking for is a way for codeigniter to convert the strings to numbers, and the find the max values of those. I don't know of any way to do this in codeigniter.
If you really want this, you have two options:
loop through all the rows in the table, and use php to parse out the number, while looking for the maximum number
Add a number column to the db, and do the string-to-number parsing in this new column whenever the values are inserted.
The first option is incredibly inefficient, and the second is probably your best bet.
I have written a PHP class that gets the headers from a .xls spreadsheet and creates a table with those headers as column names.
It also gets each row of data from the spreadsheet and places them into an array.
What I would then like to do, is determine the best data type for each column in the row. It's mostly going to be text but there will be numbers in there, say for example £1,000 this would need to be saved as 1000 and be a int rather than a string.
It needs to be done dynamically as each spreadsheet has different column names and data in different orders.
I don't really know how to go about this, I was thinking maybe a for each loop and preg_match?
Any ideas are very much appreciated.
I think that you need to check all data in column to determine if there is some not numeric values ( http://ru.php.net/manual/en/function.is-numeric.php ). If there is no not numeric data, you may use INT/TINYINT/MEDIUMINT type with appropriate length. If there is not only numeric data, you may use CHAR/VARCHAR/BLOG/TEXT with appropriate length.
The way I would go about it is to define how precise I wanted to be. For example, if I find a column with 1s and 0s, will I define it as binary or should I define it as integer just in case there will be numbers different than 0 and 1 in the future.
Also are you going to parse all the rows of the spreadsheet or only a few rows at the top before deciding which data type to use? In the above example, you may have 0s and 1s at the top of the spreadsheet, but find other numbers closer to the bottom. If you decide to review only the top rows, you may want to be less strict on the data type. So if you find only 0s and 1s, you may decide to define the field as integer, not binary. This would reduce the chances for errors when importing the data.
You could use a logic somewhat like this:
for each row (and you can decide if you want to check all the rows or just a few)
if is_int() -> data field integer;
if is_float() -> data field is float;
if is_string()
if it is a date & time -> data field is datetime;
if it is a date without time -> data field is date;
else -> data field is varchar.
I hope this helps. Good luck.
I have some PHP code which allows me to sort a column into ascending and descending order (upon click of a table row title), which is good. It works perfectly for my D.O.B colum (with date/time field type), but not for a quantity column.
For example, I have quantites of 10, 50, 100, 30 and another 100.
The order seems to be only appreciating the 1st integer, so my sorting of the column ends up in this order: 10, 100, 100, 30, 50... and 50, 30, 100, 100, 10.
This is obviously incorrect as 100 is bigger than 50, therefore both 100 values should appear at the end surely? It seems to me that 100 is only being taken into account as having the '1' value, then it appears before 10 because the system recognises it has another 0.
Is this normal to happen? Is there any way I can solve this problem? Thanks for any help. P.S. I can show code if necessary, but would like to know if this is a common issue by default.
Sounds like you're sorting by considering those values as strings, instead of as numbers. What's doing the sorting? PHP, or MySQL? Hard to say what's wrong without seeing the code that's actually sorting it.
Your field is probably defined as char/varchar/text/..., i.e. as string type and not a numeric type. In that case lexicographical sorting performed.
The sort is treating your data as strings instead of integers. Check the datatype of your column in the database. If that is a number and not a string, then the data is being converted to strings somewhere between the database and the sort routine.
looks like you are sorting strings and not numbers, what is the data type of the column? The only way to sort string numbers properly is to add leading zeros, but it is best to store that data in the proper type, so you don't have to do these sort of manipulations each time you select the data.
In the event that you need to leave the column as a character type for some reason, you can cast to a suitable numeric type for the ordering. This is less efficient than just changing the column though.
ORDER BY CAST(theColumn AS DECIMAL(5, 2))
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
Your column is a text type. You can change the column type permanently, or cast it when you do the sort.
Table alter (you may want a different default value)
ALTER TABLE [Table]
MODIFY COLUMN [Column] INTEGER UNSIGNED NOT NULL DEFAULT '0';
Or cast when you sort
SELECT *
FROM [Table]
ORDER BY CAST([Column] as UNSIGNED INTEGER) ASC;