Is it possible to insert hexa values in a SQL query? - mysql

I have a form that takes names (in natural language) as default argument and compare those to a table of users.
Is it possible to send those names in their hexadecimal value equivalent ? Example: for user "sam" i would send 0x730x610x6d or 0x73616d, but neither of them works.
Any idea ?
p.s: in my target page for this form, i defined 'charset' parameter as "utf8"

Yes it is possible. You can do the below. You can use UNHEX() function to convert a hexstring with hex pairs to binary and HEX() to do the other way round.
HEX method used to convert each characters of the string to hexa decimal number.
UNHEX method used to convert each hexa decimal numbers to characters.
Try this :
INSERT INTO tbl (col) VALUES (UNHEX('4D2AFF'))
and
SELECT HEX(col) FROM tbl

Related

Strange answer behavior with SELECT

I have a simple table using a non auto inc INT as primary key.
When querying the table with condition e.g. WHERE id='2,5,6' (unintentionally!) it returns a result set!
Ok, it works, but why?
id is an integer and you compare it with a string '2,5,6'. MySQL converts the string to a number in order to compare the two.
Well, '2,5,6' isn't a number and other DBMS would throw an error. But MySQL uses another approach: it converts character per character until the string is ended or the character is not numeric. So it sees the 2 then the comma. Depending on your settings the comma is the dicimal separater or not. So MySQL either converts to 2 or to 2.5.
Here is the documentation on implicit conversions in MySQL: https://dev.mysql.com/doc/refman/5.5/en/type-conversion.html.
The algorithm on how to convert a string to a number is not explicitly described there, but they say for instance
there are many different strings that may convert to the value 1, such as '1', ' 1', or '1a'.
They also point out in that document that implicit conversion is dangerous, because strings are not converted to DECIMAL (as I would have thought), but to the approximate datatype DOUBLE. So in MySQL we should always avoid implicit conversion from string to number.

String to Blob comparison in MySQL where clause

I have a table called messages with a column (BLOB) called message_text. I'm passing a value from the web app when a user sends a new message and I want to check if it's an exact duplicate message text.
SELECT count(message_id) FROM messages WHERE message_text = '$msgTxt' AND user_id = $userId
Where $msgTxt will be a formatted string like...
"Hello there. I don\'t know you.
I\'ve just made a new line. "
The problem is that the comparison isn't working and I'm never finding duplicates. Even if I literally copy/paste an existing value from the database and replace it with $msgTxt in my query I never get any results, and so I'm assuming there's something wrong with the way I'm comparing a blob to a string.
BLOB values are treated as binary strings (byte strings). They have the binary character set and collation, and comparison and sorting are based on the numeric values of the bytes in column values. String or Text values are treated as nonbinary strings (character strings). They have a character set other than binary, and values are sorted and compared based on the collation of the character set.
So, you have to convert either BLOB to String or String to BLOB and then compare both.
If you are using java,
Convert Blob to String
byte[] bdata = blob.getBytes(1, (int)blob.length());
String data1 = new String(bdata);
What API are you using to call MySQL? I see some backslashes, but need to verify that \ is not turning into \\, and that other escapings are not unnecessarily happening or not happening.
Which OS are you using? Windows, when reading stuff, likes to convert NL into CRLF, thereby making it so that it won't match.

Mysql convert varchar binary representation field to binary to do hamming distance calculation with bit_count

I've a db table with a varchar(64) field to store PHashing data, as 64 chars (1's and 0's ascii characters). I need to calculate hamming distance with a test hasta, and it seems that the most efficient way to do is using mysql bit_count function. The problem is that I haven't found any way to convert/cast/whatever the hash field in order to be interpreted as a byte(8) instead of varchar(64). Like:
> select hash from data;
"0000000101100111111100011110000011100000111100011011111110011011"
> select convert_to_binary(hash) from data;
0b0000000101100111111100011110000011100000111100011011111110011011
I cannot alter the data and convert all previous data into a binary field. Is there any way to force mysql to re-interpret a field as a literal, or any other alternative solution?
I think you should be able to use it like this:
SELECT BIT_COUNT(CAST(hash AS BINARY)) FROM data;
Use CAST to convert the field to BINARY and pass the result to BIT_COUNT. Casting the field hash to binary will turn it into a string with the binary data type, but BIT_COUNT is able to handle that.
Working code:
SELECT BIT_COUNT( CONV( hash, 2, 10 ) ^
0b0000000101100111111100011110000011100000111100011011111110011011 )

how mysql treat the hex in sql statement

I know a little SQL injection about php and i read some posts about sqli!the posts say when hackers want to get the column names of a table,they will use
select group_concat(column_name) from information_schema.columns where table_name=0x7573657273
in the statement "0x7573657273" is the hex string of "users".surprisingly,when I execute this statement in mysql console,it will exactly return the colunms of table users.
does it mean mysql will automately convert hex to string,eg convert "0x7573657273" to "uses" or convert "0x3D" to "="?
does it mean mysql can understand what the hex means?
In string contexts, MySQL treats hexadecimal values as binary strings, where each pair of hex digits is converted to a character.
Refer link: http://dev.mysql.com/doc/refman/5.0/en/hexadecimal-literals.html

SQL Server finding string in Unicode HEX

I need to find part of string which is stored in a varbinary field on SQL Server 2008. The data that is stored in the field is in HEX Binary.
For example the field has 0xA0000000001A000000000000000000000000000000000000000000000000000000850000002416002F002A002000480065006C006C006F0020002A002F0007
I can ignore the first 37 characters. When I look at this in a SQL application it reads as follows after the first 37 characters
/..H.e.l.l.o../
I know that the hex binary is stored in Unicode format.
My question is how can i search for the word 'Hello' using a SQL Statement.
I tried the following below but i do not get any results
SELECT CONVERT(varchar(max),fieldname) from tablename where fieldname like '%Hello%;
I would really appreciate any help that I can get
Thanks
If the binary value is in a Unicode encoding, you will need to cast it to NVARCHAR, and also use a Unicode string literal for the search value:
SELECT CONVERT(nvarchar(max),fieldname) from tablename where fieldname like N'%Hello%';