I am trying to insert a UTC timestamp into a row in mysql by using MySQLWorkBench. I have set the column type as TIMESTAMP, BIGINT, INT and TIME. No matter what I set it as I receive the same error - "incorrect (Type Here) value".
I am using this code to test this:
INSERT INTO mytablenamehere (`Time`) VALUES ('UNIX_TIMESTAMP(NOW(3))')
Should add I am on MYSQL 8.0. What am I doing wrong?
You're trying to insert a string. Your query should be VALUES (UNIX_TIMESTAMP(NOW(3))) (without the single quotes). Also note that (from the docs):
The return value is an integer if no argument is given or the argument does not include a fractional seconds part, or DECIMAL if an argument is given that includes a fractional seconds part.
So your column must be either an INT or a DOUBLE, depending on the precision you want.
Related
I have this tabular structure:
I get an error when i try to insert a specific set of value, query with those values is as below:
For longitude with value -161.9722021 it is throwing me an error, values less than 100 value were successfully inserted.
I don't see the value being out of range, the value i am giving in the query is not a string and is a decimal value as specified. I have checked other similar questions on the internet, but they actually had a value out of range. I am still unable to figure out why I am getting this error.
decimal(10,8) means that you have ten digits, and eight of those are for the decimal part.
This leaves two digits only for the integer part.
And that is why "values less than 100 are successfully inserted" -- because their integer part only takes two digits.
Use either DECIMAL(11,8) (same precision) or DECIMAL(11,7) (same size).
During liquibase script execution I'm filling the table with data. Data has one column with the next value: 2017-10-03T07:11:00.0000000Z which I'm struggling with to parse into the column:
liquibase.exception.MigrationFailedException: Migration failed for change set /changelog/00000000000001.xml::00000000000001::IAmUser:
Reason: java.lang.NumberFormatException: For input string: "0000000Z"
I've tried multiple column types but none of them worked: datetime, datetime2, timestamp (datetimeoffset doesn't work with MySQL).
How can I parse this date into SQL column?
Use datetime(6) / timestamp(6) where 6 is the precision for fractional seconds. Also, leave out the Z from the end of the date string.
Let's say y and z are columns in x, does the insertion of data of type INT need to be inside apostrophes?
INSERT INTO x (y, ..., z)
VALUES (4, ..., 9);
or
VALUES ('4', ..., '9');
Does the second example insert the values as characters?
I've tested the inserts through literal use phpMyAdmin's GUI to find that they do inserts of type INT with apostrophes but I'm not sure if that's the syntax displayed in the console when using the GUI. I've also tested through a PHP script without apostrophes and had a successful insertion so I'm not sure.
The column type determines the variable type, not the quotes/apostrophes. The apostrophes can be used on integers, it won't break anything, but they are not required as long as the column is an int.
If the column is an int column, then it will only store numeric values, not string characters. MySQL knows that '1' is a 1 when you're inserting into a numeric column. So no, when inserting into a numeric column, the number doesn't need to be in quotes.
If you try to insert an actual string character value into an int column, you get an error. eg:
mysql> insert into test values('a');
ERROR 1366 (HY000): Incorrect integer value: 'a' for column 'id' at row 1
There's a lot more that could be said on this topic, relating to how strings and integers are actually stored in binary. How the values are stored and converted is called the character set - you've probably heard of things like "utf-8" or ASCII. MySQL has the capability to convert between these (http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html), but that's probably beyond the scope of this question :)
I am inserting decimals into my MYSQL database but the values that end up in the DB are rounded, despite my having set the DB to DECIMAL(10,4). For example I set the discount to 6.5 and the DB records it as 7.
My query is:
$save = sprintf("
INSERT INTO discount
SET am_discount = %d,
tx_discount = '%s'
",
$_POST['am_discount'],
mysql_escape_string($_POST['tx_discount'])
);
I've tried setting the am_discount field to %u and %f as well, but it doesn't seem to make a difference.
As I mentioned, the am_discount field in my MYSQL DB is set to DECIMAL (10,4) and I've also tried (10,2) and (8,2).
What am I missing?
Try :
$save = sprintf("INSERT INTO discount SET am_discount = '".$_POST['am_discount']."',
tx_discount = '".mysql_escape_string($_POST['tx_discount'])."' ");
its seem to be database table column property issue, if some one can endorse me on it.
please change your table column property to "Float".
MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, “(M,D)” means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.
Reference :
http://dev.mysql.com/doc/refman/5.0/en/floating-point-types.html
It is not a database table column property issue. Decimal is the correct column type.
For inserts into a DECIMAL or integer column, the target is an exact data type, so rounding uses “round half away from zero,” regardless of whether the value to be inserted is exact or approximate.
See http://dev.mysql.com/doc/refman/5.0/en/precision-math-rounding.html for details.
This has nothing to do with the database, actually. It's sprintf behavior for %d.
%d The argument is treated as an integer and presented as a (signed) decimal number.
From the MySQL reference manual:
If you store a number into an ENUM column, the number is treated as the index into the possible values, and the value stored is the enumeration member with that index.
But when I try to write an SQL_SMALLINT value to that ENUM column using ODBC I get the error HY000:1:1265 (Data truncated for column ...).
So how can I write a number into a MySQL ENUM column using ODBC, such that this number is interpreted as the enum index?
Edit: Some more information
The column is defined as:
`TrackState` ENUM('NEWE','NEWN','VALID','INVISIBLE','CLOSED','DIED') NULL
and the statement used with ODBC is:
INSERT INTO test (TrackState) VALUES (?)
and in my C code I use SQLBindParameter with SQL_C_USHORT and SQL_SMALLINT to bind an unsigned short variable.
When I change my TrackState column to an ordinary INT column, the numeric value (which I want to be interpreted as the index of an enum) is successfully written.