While reading an oracle view from php :ORA-06502: PL/SQL: numeric or value error: character to number conversion error - php

this query it works!
SELECT largdisp FROM vcantieri WHERE SEQ_EVENTO=97161
(value of largdisp is 600 and it works for any value 500,600,700...1000 or null)
this query gives an ERROR! ORA-06502
SELECT largdisp FROM vcantieri WHERE SEQ_EVENTO=97195
(value of largdisp 650 and not works for any value 750,850,950....)
largdisp is a decimal field in original table, converted in varchar2 in view
IN TOAD this query it works always!!!

Related

MySql/mariadb - SELECT 0 = 'N;' returns true [duplicate]

This question already has answers here:
mysql: why comparing a 'string' to 0 gives true?
(3 answers)
Closed 3 days ago.
I was going through some MySql (mariadb) report looking for why I was getting too many rows in a report and found that the column being queried which stores serialized php and matches another column value is falsely matching.
where m.section = SUBSTRING_INDEX(SUBSTRING_INDEX(s.other,CHAR(59),2),CHAR(58),-1)
This is fine. m.section contains a number. s.other might have a value a:1:{s:3:"foo";i:4;}, so it is matching the 4 in that data.
Then I found that some n.other records are storing null. Php's serialize outputs a capital N followed by a semicolon to represent a null.
echo serialize(null); // outputs N;
echo is_null(unserialize('N;')); // outputs 1
So php thinks N; is null.
In my query, when m.section equals zero, it is matching the record. Zero equals php null? Lets test that.
SELECT 0 = 'N;'
outputs
1 !
What else equals null that shouldn't?
select 1 = 'N;' --> 0
select 0 = null --> null
select 0 is null --> 0
select 'N;' is null --> 0
Am I going mad? How come zero equals php's serial null?
Its not PHP, its MariaDB.
Through the type conversion rules, 'N;' is converted (badly) to DECIMAL.
During the conversion it gets a 0 value. And with the equality test to 0 becomes true.
Looking at the warnings of the select 0 = 'N;' will result in:
Warning 1292 Truncated incorrect DECIMAL value: 'N;'
ref: https://dbfiddle.uk/4rZ0N8CF

MySQL updates all rows when the column name is only specified in the where clause

It's been a while I didn't work on mysql and I was suprised to see that the following statement is valid:
UPDATE table_A
SET col_a = value
WHERE id;
It looks like MySQL updates all rows in the table. I have a good MSSQL background and I am trying to understand why this valid in MySql?
Also, I ran a query with a similar where clause on a varchar column:
SELECT distinct description
FROM table_A
where NOT description;
I tought it will return only null or empty values. Instead, it returns lots of rows with non-null values.
Any ideas why?
Thanks,
WHERE x will match any rows for which x evaluates to a truthy value. Since BOOLEAN is actually a number type TINYINT(1), this works by converting to a number and then comparing to zero - any nonzero number is truthy. (NULL is falsy.)
If you write WHERE id = 123, then only for the row where id is 123, the expression id = 123 will evaluate to TRUE (which is the same as 1) and it will match, otherwise it will evaluate to FALSE (0).
But if you write WHERE id, the requirement is that id evaluates to a truthy value. If id is a number, only IDs 0 and NULL will be falsy.
However, in case of description, you have a string. And the string is first converted to a number. The reason you got many results there is that any string that starts with a number (that is nonzero) is matching, such as 01234hello (which converts to 1234, which is nonzero). Check what CONVERT(description, SIGNED) gives - if it is nonzero, then it matches.
This is why, when building AND or OR queries in code, you can avoid handling the case of zero conditions specially by starting with TRUE or 1 (in the AND case) or FALSE or 0 (in the OR case), since WHERE TRUE/WHERE 1 is valid (matches everything), as is WHERE FALSE/WHERE 0 (matches nothing). So you build a query by starting with WHERE 1 and adding to it: WHERE 1, WHERE 1 AND id = 123, WHERE 1 AND id = 123 AND type = 'xy', etc.

Convert number value from php to varbinary in mysql

In MYSQL database, I have a varbinary(255) field.
In my PHP script, I am trying to pass the input of string (either in decimal or hex) to MYSQL to save the value in varbinary(255). I tried passing the value in decimal, hexadecimal, and binary, but none of them gave me the correct value.
$val = "0x54321"; // Used hardcoded hex value here, but it can be also decimal in my code
$v_dec = intval($val,0); // That's why I have intval() here
$v_hex = dechex(intval($val,0));
$v_bin = decbin(intval($val,0));
$v_bin_2 = (binary)intval($val,0);
I tried inserting these values using regular INSERT query, but each of them gave weird numbers.
Variable Converted Value (in php) varbinary Value (in MYSQL)
v_dec: 344865 -> 333434383635
v_hex: 54321 -> 3534333231
v_bin: 1010100001100100001 -> 31303130313030303031313030313030303031
v_bin2: 344865 -> 333434383635
What data type should the value be in? I want to do the conversion in php instead of using CAST() in the MYSQL query. Thanks!
EDIT:
What I want to do is to correctly display the saved varbinary value in hex.
EDIT:
Found the problem. The problem was how I was retrieving the value. Instead of HEX(value), I retrieved it as BIN(value) in mysql level and then converted it into hex with dechex(bindec($value)) in php.

querying VARCHAR2 with MAX in ORACLE

I have a column CLIENT_ID with datatype VARCHAR2
the data ranged from CL000001 to CL000163
so when i query the maximum value with,
select max(to_number(replace(client_id,'CL'))) from mst_client
I am onlly getting 163 as the max value,
I intend to get the CL000163 for the maximum value. What did i do wrong here ??
You dont need to change to to_number a direct max will give the output,
SCOTT#research 13-APR-15> select * from maxtest;
A
----------
CL000001
CL000002
CL000003
CL000004
SCOTT#research 13-APR-15> select max(a) from maxtest;
MAX(A)
----------
CL000004

Mysql not inserting binary data properly

I am working on data compression and for some reason i need only 8 bits.
I am converting number by decbin() and then inserting it in the mysql, the mysql column data type BIT width is 8 bit. I used mysql_query("INSERT INTO n (reading) VALUES (b'".$value."')") and tried this one toomysql_query("INSERT INTO n (reading) VALUES (".$value.")"). Before inserting the value is fine but after insertion its not the same value, it changes the value for example before insertion it echo the value 116 then I echo its binary value 1110100 and it insert the value in mysql column is 00110000.
function delta($reading){
global $flag;
$delta = $flag - $reading;
saveDelta(decbin($delta));
}
here is the other function where it saves the value
function saveDelta($dif) {
mysql_query("INSERT INTO n (reading) VALUES (".$dif.")");
}
The syntax "INSERT INTO n (reading) VALUES (b'".$value."')" should work provided that $value is properly encoded as a string of '0' and '1'.
EDIT: I noticed you didn't provide any "sequence number" while inserting your data. But, please remember that without using a proper ORDER BY clause, you cannot retrieve your bytes the order they where entered at first. Maybe you think you read "116" but MySQL return an other row from the table?
Here are some usage examples, First using the BIT type:
CREATE TABLE b (value BIT(8));
INSERT INTO b VALUES (0),(1), (255);
INSERT INTO b VALUES (b'00000000'),(b'00000001'), (b'11111111');
Please note that when retrieving BIT columns you will obtain signed result (i.e.: storing 255 will read -1).
You could retrieve your data either as signed 10-base integers or binary form (with optional padding):
SELECT value FROM b;
SELECT BIN(value) FROM b;
SELECT LPAD(BIN(value), 8, '0') FROM b;
As of myself, I would prefer the TINYINT UNSIGNED. This is an 8 bit type which support the same syntax for values (either <10-base digit> or b'xxxxxxxx') -- but will accept the UNSIGNED specifier:
CREATE TABLE t (value TINYINT UNSIGNED);
INSERT INTO t VALUES (0),(1),(255);
INSERT INTO t VALUES (b'00000000'),(b'00000001'), (b'11111111');
You could retrieve your data either as unsigned 10-base integers or binary form (with optional padding):
SELECT value FROM t;
SELECT BIN(value) FROM t;
SELECT LPAD(BIN(value), 8, '0') FROM t;
See http://sqlfiddle.com/#!2/4ff44/6 to experiment with both of them.

Categories