I have a column idvisitor in my MySQL table with type binary(8).
How can I cast this to a char in order to concat it with a string?
Example:
A query returns 0x652B61CB9B4FD13B for idvisitor.
When I run concat('h', v.idvisitor) then I get 0x68652B61CB9B4FD13B but I need h652b61cb9b4fd13b
I found the solution in the Matomo documentation:
concat('h', conv(hex(idvisitor), 16, 16))
Related
I am new in Mysql and have a table where I'm going to select based on an integer column, the problem is that when I use an string on this column! I got no error but it gives me back all rows. for example:
SELECT * FROM `News` WHERE Cat='hello' order by id desc limit 20
It gives me 20 rows! what's wrong? did I do anything wrong or it's because of something else?
This is normal behavior for MySql, because in this expression:
Cat='hello'
what happens is an implicit conversion of the string literal 'hello' to INTEGER and as it is described in Type Conversion in Expression Evaluation the result of this conversion is 0, so the expression is equivalent to:
Cat=0
If you want to prevent this conversion you could instead convert the column Cat to string:
WHERE CONVERT(Cat, CHAR) = 'hello'
This way the comparison of Cat and 'hello' will be alphanumerical and will fail.
But if you pass a valid integer, then the correct result will be returned.
Why if I run a query with gibberish at the end of the where condition it incorrectly returns a result.
Example:
SELECT * FROM contractor_table WHERE contractorID = '97sd'
I am getting the row with the ID 97, when I should get no result.
What is happening here is that you are comparing the contractorID column, which is integer or some other numeric type, against a string literal 97sd. This doesn't make any sense, so MySQL, based on its casting rules, attempts to first cast 97sd to an integer. Consider the result of the following query:
SELECT CAST('97sd' AS unsigned);
In fact, this outputs just 97, the integer. So, in practice this means that the "gibberish" at the end of your string literal, which begins with an integer, will be ignored by MySQL.
But best practice here is to always compare columns against the correct literal types. So use the following version always, for best results:
SELECT * FROM contractor_table WHERE contractorID = 97;
This happends when you have column type int or other numeric if you convert it into varchar than it will retun no output
This sql returns correct result:
select * from `user` where `profile`->"$.year" IN ("2001")
But when I add more than one values
select * from `user` where `profile`->"$.year" IN ("2001", "1")
returns empty
It seems "In" statement not working as expected on JSON column in Mysql 5.7?
See documentation:
11.6 The JSON Data Type
...
Comparison and Ordering of JSON Values
...
The following comparison operators and functions are not yet
supported with JSON values:
BETWEEN
IN()
GREATEST()
LEAST()
A workaround for the comparison operators and functions just listed is
to cast JSON values to a native MySQL numeric or string data type so
they have a consistent non-JSON scalar type.
...
Try:
SELECT *
FROM `user`
WHERE CAST(`profile` -> "$.year" AS UNSIGNED) IN ("2001", "1");
See dbfiddle.
condition in below query returning true result but i want false result. Please suggest me.
select *
from test
where
'00160001' between '0013001' and '0023000' OR
'00200000' between '0013001' and '0023000'
You get the same result without the leading zeros.
Use integer data type to compare integer values.
You can also convert the values on the fly using cast:
http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html
Your example with Cast:
select if( cast('00160001' as unsigned) between cast('0013001' as unsigned)
and cast('0023000' as unsigned),1,0) as test_a,
if( cast('00200000' as unsigned) between cast('0013001' as unsigned)
and cast('0023000' as unsigned),1,0) as test_b;
Let's say I have a json column fields, like so:
{phone: 5555555555, address: "55 awesome street", hair_color: "green"}
What I would like to do is update all entries where the json key phone is present, and the result is of type number to be a string.
What I have is:
SELECT *
FROM parent_object
WHERE (fields->'phone') IS NOT NULL;
Unfortunately this still returns values where phone:null. I'm guessing that a JSON null is not equivalent to a SQL NULL.
How do I
1) How do I rule out JSON nulls
AND (fields->'phone') <> null produces
LINE 4: ...phone') IS NOT NULL AND (fields->'phone') <> 'null';
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
2) Check the type of the value at that key, this pseudocode (type_of (fields->'phone') == Integer) but in working PGSQL.
3) Modify this to update the column
UPDATE parent_object
SET fields.phone = to_char(fields.phone)
WHERE query defined above
As other folks have said, there is no reason to convert the variable to an integer just to them cast it to a string. Also, phone numbers are not numbers. :-)
You need to be using the ->> operator instead of ->. That alongside IS NOT NULL gets your SELECT query working.
Note the difference between the two tuple values after running this query:
SELECT fields->'phone', fields->>'phone'
FROM parent_object;
Your working query:
SELECT *
FROM parent_object
WHERE (fields->>'phone') IS NOT NULL;
Postgres does not currently natively support atomically updating individual keys within a JSON column. You can write wrapper UDFs to provide this capability to you: How do I modify fields inside the new PostgreSQL JSON datatype?
For checking the type of the value at key, postgres has the following in the documentation.
json_typeof ( json ) → text
jsonb_typeof ( jsonb ) → text
Returns the type of the top-level JSON value as a text string. Possible types are object, array, string, number, boolean, and null. (The null result should not be confused with a SQL NULL; see the examples.)
json_typeof('-123.4') → number
json_typeof('null'::json) → null
json_typeof(NULL::json) IS NULL → t