Anonym
Anonym

Reputation: 7735

How do I cast a type to a bigint in MySQL?

CAST() seems to only work for BINARY,CHAR,DATE;DATETIME,DECIMAL,TIME,SIGNED,UNSIGNED.

I need to convert a hex string to a bigint, that is, I'd want:

SELECT CAST(CONV("55244A5562C5566354',16,10) AS BIGINT)

CONV() returns a string, so that's why I'm trying the convert it. I have 2 uses for this

Upvotes: 26

Views: 71035

Answers (2)

vartec
vartec

Reputation: 134711

What seems to be the problem? I've tested this conversion both on 64-bit and 32-bit system. Works fine. Note, that instead of doing hex to bin conversion, you can just treat the number as hexadecimal.

mysql> SELECT CAST(X'55244A5562C5566354' AS UNSIGNED);
+-----------------------------------------+
| CAST(X'55244A5562C5566354' AS UNSIGNED) |
+-----------------------------------------+
|                     2614996416347923284 |
+-----------------------------------------+
1 row in set (0.00 sec)

Upvotes: 3

Quassnoi
Quassnoi

Reputation: 425803

SELECT CAST(CONV('55244A5562C5566354',16,10) AS UNSIGNED INTEGER);

Upvotes: 42

Related Questions