Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting hex value in MySQL

I have created an SQL database using Java. I have a table created which has two columns, the first being a big integer which increments, the second I have tried defining it as a char, varchar and binary.

But I'm still not getting the desired functionality. Say I try and store a hex number 0a into the char column and I get an error. I appended 0x to the beginning and it seems to store, but when I print out the contents it is blank. Or in some cases I get characters such as '/' or '?'. I also tried using SQL explorer and it gives me the same result viewing the table,

My problem is I need to store an eight character hex string such as eb8d4ee6.

Could someone please advise me of how this can be done?

like image 984
user524156 Avatar asked Nov 29 '10 17:11

user524156


People also ask

What is hex in MySQL?

HEX() : This function in MySQL is used to return an equivalent hexadecimal string value of a string or numeric Input. If the input is a string then each byte of each character in the string is converted to two hexadecimal digits.

What is 0x in MySQL?

The 0x notation is based on ODBC, for which hexadecimal strings are often used to supply values for BLOB columns.

What does 0x mean in SQL Server?

0x denotes that the value is binary, which is displayed using hex values.

What is binary data type in MySQL?

The BINARY attribute is a nonstandard MySQL extension that is shorthand for specifying the binary ( _bin ) collation of the column character set (or of the table default character set if no column character set is specified). In this case, comparison and sorting are based on numeric character code values.


1 Answers

See http://dev.mysql.com/doc/refman/5.5/en/hexadecimal-literals.html

MySQL supports hexadecimal values, written using X'val', x'val', or 0xval format, where val contains hexadecimal digits (0..9, A..F). Lettercase of the digits does not matter. For values written using X'val' or x'val' format, val must contain an even number of digits. For values written using 0xval syntax, values that contain an odd number of digits are treated as having an extra leading 0. For example, 0x0a and 0xaaa are interpreted as 0x0a and 0x0aaa.

In numeric contexts, hexadecimal values act like integers (64-bit precision). In string contexts, they act like binary strings, where each pair of hex digits is converted to a character:

You probably should store the Hex number in an integer column. You can then convert back to hex when selecting using the HEX() function.

E.g.,

INSERT INTO MyTable (`MyIntegerColumn`) VALUES (0xeb8d4ee6);
like image 193
D'Arcy Rittich Avatar answered Oct 06 '22 00:10

D'Arcy Rittich