I have a table with columns
tick varchar(10), category varchar(100)
and they have the following 2 records (for sample)
EFG,0
XYZ,#
(EFG and XYZ) are values for tick column, and (0 and #) are for categories column, respectively.
My need is to append the string ',Cool,' (commas included) to the existing category value, so that the above two records become
"EFG","0,Cool,"
"XYZ","#,Cool,"
So I am running the following SQL statements:
update tablen set category = category + ',Cool,' where tick='EFG';
update tablen set category = category + ',Cool,' where tick='XYZ';
However, for the first SQL statement, it is not changing any value at all. For the second, the category value is updated from # to "0"
In both cases, I am getting warnings:
Warning: #1292 Truncated incorrect DOUBLE value: '#' (and for '0')
Warning: #1292 Truncated incorrect DOUBLE value: ',Cool,'
Unable to understand why the VARCHAR column update is throwing a DOUBLE value error.
Can someone please shed some light on what am I doing wrong here? Is there any typecasting required?
Many Thanks
in mysql the concat is not + but concat()
update tablen set category = concat(category , ',Cool,') where tick='EFG';
update tablen set category = concat(category , ',Cool,') where tick='XYZ';
the plus sign induce mysql to think that the values are number .. but you have varchar ..
Related
I have MySQL installed locally, running SELECT VERSION() returns this value: 5.6.43-84.3
When I run a query it is returning multiple rows when it should only return 1 row. Let me set it up, it's easier to explain that way.
Create a test table:
CREATE TABLE test_table
(
test_val VARCHAR(255)
)
;
Load 3 values into the table:
INSERT INTO test_table (test_val)
VALUES
('9671986020630615'),
('9671986020630616'),
('9671986020630617')
;
Run this query (This query returns 1 row which is expected):
SELECT *
FROM test_table
WHERE test_val = '9671986020630615'
;
Run this query (This query returns 3 rows, which it shouldn't):
SELECT *
FROM test_table
WHERE test_val = 9671986020630615
;
Here's what I have observed about this situation:
The first query surrounds the value in the WHERE clause with single quotes.
The second query does not surround the value in the WHERE clause with single tics.
The column in the test table is defined as VARCHAR(255)
It makes sense that the first query returns just one row because it's comparing a string from the WHERE clause to a string value in the test table (VARCHAR(255))
Something is happening when MySQL compares the numerical value in the WHERE clause of the second query to the string value in the test table (VARCHAR(255)) which is causing MySQL to return 3 rows instead of just 1.
It makes sense that the first query returns the correct result because it is comparing a string to a string.
It also makes a degree of sense that the second query is returning a bad dataset (3 rows as opposed to the 1 row it should return).
But my question is why is MySQL doing this? Why when it compares a number to 3 different VARCHAR(255) values does it return all 3 rows when the true value of the numerical value in the WHERE clause only matches 1 row?
So, in essence for the first query MySQL is saying:
'9671986020630615' = '9671986020630615',
'9671986020630615' <> '9671986020630616',
'9671986020630615' <> '9671986020630617'
but for the second query it is saying:
9671986020630615 = '9671986020630615',
9671986020630615 = '9671986020630616',
9671986020630615 = '9671986020630617'
Any help will be much appreciated.
MySQL handles all numbers internally the same way Javascript does, with IEEE double-precision floating point representation.
When you omit the quotation marks from your long numeric strings, that is you write 9671986020630615 in place of '9671986020630615 ', MySQL uses the number. Then, when it runs the WHERE part of your query, it silently coerces each column value to a double precision number.
But due to the machine epsilon -- the limit of precision -- of double precision, 9671986020630615, 9671986020630616, and 9671986020630617 all have the same value. So the WHERE finds all three.
CAST(9671986020630615 AS DOUBLE) CAST(9671986020630616 AS DOUBLE) CAST(9671986020630617 AS DOUBLE)
9.671986020630616e15 9.671986020630616e15 9.671986020630616e15 |
See how all three integers have the same representation as DOUBLE?
I'm trying to do the following query
INSERT INTO 'table_1'(column_1)
SELECT DISTINCT 'column 2'
FROM other_schema.table2;
Both of those columns have their fields set to INT(255), and yet I get the following error:
"Error Code: 1366. Incorrect integer value: 'column2' for column 'column2' at row 88163"
I even went and checked the individual row, but the field is a number(5800 to be precise).
I did some tinkering, and it seems that if I try to limit the select, it always accuses the row immediately after the upper limit of being of an incorrect value.
If you use single quotes around 'column 2' sql will interpret that as a string, you need to remove the quote, or add backticks ` if the table name has spaces
SELECT DISTINCT `column 2`
The way you've written your query currently is that you're trying to INSERT a string into your integer column. When you do
SELECT DISTINCT 'column2'
you are selecting the literal string 'column2' and not the values from that column. I would unquote the references to your table and column:
INSERT INTO table
SELECT DISTINCT column2 FROM ...
It seems pretty clear to me that table2(column2) is not a number. You may think it is for some reason, but it is not.
However, table1(column1) is a number. So, one method to solve this is to use silent conversion. That is, convert the value to a number but in such a way that no error occurs. MySQL does this with + 0:
INSERT INTO table1(column1)
SELECT DISTINCT (column2 + 0)
FROM other_schema.table2;
I would suggest adding where (column2 + 0) <> 0, assuming that you never expect 0 for this column.
I should also note that you have no idea what row is 88,163 for the insert. This takes place after the distinct, so it is very unlikely that it is the row identified as 88,168th in the table.
I have installed recently MySQL 5.7 . Something weird is happening with a date column.
If I execute a SELECT query using that field in a WHERE section, I have a resultset, but when I use the same WHERE condition to UPDATE a row I get an Invalid date format error message.
Here is the SELECT sentence:
SELECT *
FROM
FDpoCargTran
WHERE FDpoCargTran.Banco = '001'
AND (FDpoCargTran.Conciliacion = '' OR FDpoCargTran.Conciliacion IS NULL)
AND FDpoCargTran.Fecha = '2016-09-27'
This sentence returns 2 rows resultset, that's ok.
Now, Here's the UPDATE sentence:
UPDATE
FDpoCargTran
SET
Edo = 'C'
WHERE FDpoCargTran.Banco = '001'
AND (FDpoCargTran.Conciliacion = '' OR FDpoCargTran.Conciliacion IS NULL)
AND FDpoCargTran.Fecha = '2016-09-27'
AND Deposito = 1041
And I get this error message:
Data truncation: Incorrect date value: '' for column 'Conciliacion'
The Conciliacion columns is defined as:
`Conciliacion` date DEFAULT NULL,
If I remove the Conciliacion = '' condition, everything works fine.
Why empty string is not valid to evaluate the column in an UPDATE sentence and not in a SELECT?
Please, an idea!!!
Basically, for date datatype, you cannot store something like White
Spaces or ' ' strings. You need to make the column to accept NULL
values and insert an actual NULL into it. This is not a problem in MySQL 5.7, its how date is set in databases.
Update is a DML statement, when you actually want to write something into table, or check for a condition, MySQL is unable to understand what ' ' is for the date column type.
So, you cannot have ' ', instead you can have NULL set. Thats how MySQL can check the condition and make appropriate changes!!
I would suggest, change it to NULL.
Alter your datetime column to varchar.
Import whatever table/database.
Update FDpoCargTran set Conciliacion=NULL where Conciliacion='';
Alter column back to datetime.
Using a SQL Server 2008 database, I am trying to find rows in column B that don't match a constant piece of data + primary key. Where primary Key is column A
Here's an example where I am looking for a SQL statement that will retrieve row 3 from this table:
Row xxxColumn AColumn B
Row 1 123 od123
Row 2 124 od124
Row 3 125 od789
Row 4 126 od126
Alias the table and compare? Create a view? Is there a way to use concatenate?
Thanks in advance
Replace my variables with whatever column names you are using...
declare #columna varchar(20)
set #columna = '125'
declare #columnb varchar(20)
set #columnb = 'od789'
select len(#columnb) - len(replace(#columnb,#columna,''))
If the result of select len(#columnb) - len(replace(#columnb,#columna,'')) = 0 you can take the result (meaning it does not exist in the string). If however the result > 0 you do not want to pick that up, for example, using your data:
declare #columna varchar(20)
set #columna = '123'
declare #columnb varchar(20)
set #columnb = 'od123'
select len(#columnb) - len(replace(#columnb,#columna,''))
Would return 3.
A more general solution could be:
SELECT <enter your columns that you want to return here>
FROM
YourTable
WHERE
len(columnb) - len(replace(columnb,columna,''))=0
The idea here is if there is no replacement to be done you end up with the length of the original string (column b) subtracted from the original string (again column b, since nothing has changed), this is the data you want returned (WHERE... = 0).
On the other hand if a replacement is made within the string you end up getting a positive length (even if the strings are exact, try it with od123 and od123), this in turn should be discarded from your result set. This is why I filtered in the where condition using = 0.
After changing the data type of a MySql column in order to store Twilio call ids (34 char strings), I try to manually change the data in that column with:
update calls
set incoming_Cid='CA9321a83241035b4c3d3e7a4f7aa6970d'
where id='1';
However I get an error which doesn't make sense seeing as the column's data type was properly modified?
| Level ||| Code | Message
| Warning | 1265 | Data truncated for column 'incoming_Cid' at row 1
Your problem is that at the moment your incoming_Cid column defined as CHAR(1) when it should be CHAR(34).
To fix this just issue this command to change your columns' length from 1 to 34
ALTER TABLE calls CHANGE incoming_Cid incoming_Cid CHAR(34);
Here is SQLFiddle demo
I had the same problem because of an table column which was defined as ENUM('x','y','z') and later on I was trying to save the value 'a' into this column, thus I got the mentioned error.
Solved by altering the table column definition and added value 'a' into the enum set.
By issuing this statement:
ALTER TABLES call MODIFY incoming_Cid CHAR;
... you omitted the length parameter. Your query was therefore equivalent to:
ALTER TABLE calls MODIFY incoming_Cid CHAR(1);
You must specify the field size for sizes larger than 1:
ALTER TABLE calls MODIFY incoming_Cid CHAR(34);
However I get an error which doesn't make sense seeing as the column's data type was properly modified?
| Level | Code | Msg | Warn | 12 | Data truncated for column 'incoming_Cid' at row 1
You can often get this message when you are doing something like the following:
REPLACE INTO table2 (SELECT * FROM table1);
Resulted in our case in the following error:
SQL Exception: Data truncated for column 'level' at row 1
The problem turned out to be column misalignment that resulted in a tinyint trying to be stored in a datetime field or vice versa.
In my case it was a table with an ENUM that accepts the days of the week as integers (0 to 6). When inserting the value 0 as an integer I got the error message "Data truncated for column ..." so to fix it I had to cast the integer to a string. So instead of:
$item->day = 0;
I had to do;
$item->day = (string) 0;
It looks silly to cast the zero like that but in my case it was in a Laravel factory, and I had to write it like this:
$factory->define(App\Schedule::class, function (Faker $faker) {
return [
'day' => (string) $faker->numberBetween(0, 6),
//
];
});
I had the same problem, with a database field with type "SET" which is an enum type.
I tried to add a value which was not in that list.
The value I tried to add had the decimal value 256, but the enum list only had 8 values.
1: 1 -> A
2: 2 -> B
3: 4 -> C
4: 8 -> D
5: 16 -> E
6: 32 -> F
7: 64 -> G
8: 128 -> H
So I just had to add the additional value to the field.
Reading this documentation entry helped me to understand the problem.
MySQL stores SET values numerically, with the low-order bit of the
stored value corresponding to the first set member. If you retrieve a
SET value in a numeric context, the value retrieved has bits set
corresponding to the set members that make up the column value. For
example, you can retrieve numeric values from a SET column like this:
mysql> SELECT set_col+0 FROM tbl_name; If a number is stored into a
If a number is stored into a SET column, the bits that are set in the
binary representation of the number determine the set members in the
column value. For a column specified as SET('a','b','c','d'), the
members have the following decimal and binary values.
SET Member Decimal Value Binary Value
'a' 1 0001
'b' 2 0010
'c' 4 0100
'd' 8 1000
If you assign a value of 9 to this column, that is 1001 in binary, so
the first and fourth SET value members 'a' and 'd' are selected and
the resulting value is 'a,d'.
when i first tried to import csv into mysql , i got the same error , and then i figured out mysql table i created doesn't have the character length of the importing csv field ,
so if it's the first time importing csv
its a good idea to give more character length .
label all fields as varchar or text , don't blend int or other values.
then you are good to go.
Check whether you are using the 'enum' datatype.If you are using 'enum' datatype then the items inside the enum datatype should be exactly match with the data which you are entering.Ex.
You are taking the enum datatype like this:
enum('book',stationery','others')
then when you are inserting the data into the database you have to do like this:
INSERT INTO database_name.table_name (column1,...columnn) VALUES().
THE value should include same items which you are mentioned within the bracket of enum datatype.
my issue was I used single quote instead of double quotes which add extra words in Boolean column
I Faced the same issue .In my case i was inserting a empty string for a numeric column.But by inserting a numeric value in string form for same column it got resolved e.g '12.56' --> numeric column will work but '' --> numeric column will give the above mentioned error.
Note: For numeric columns in MySql we can pass values in double quotes also .
In some cases this can be result of wrong type of input. For example, you have a column decimal(10,2) and the input isn't sanitized and is 7,7 but should be 7.7.