MySQLではBIGINTするバイナリ文字列に変換?
質問
私は、MySQLでの64ビット値(BIGINT)に文字列をハッシュしようとしています。私は、バイナリ文字列として128ビットのハッシュを返すMD5()関数を知っています。私はちょうど底部またはその結果の上位64ビットを取ることが幸せになると思います。しかし、私はあらゆる種類の数値型にバイナリ文字列型から取得する方法を見つけ出すことはできません。任意のポインタ?
解決
数に変換する10とCONV()
をベースにベース16からのMD5ハッシュを変換するCAST
関数を使用します:
select cast(conv(substring(md5(id), 1, 16), 16, 10) as unsigned integer) from SomeTable;
他のヒント
CREATE FUNCTION dbo.HexStrToVarBinary(@hexstr varchar(8000))
RETURNS varbinary(8000)
AS
BEGIN
DECLARE @hex char(1), @i int, @place bigint, @a bigint
SET @i = LEN(@hexstr)
set @place = convert(bigint,1)
SET @a = convert(bigint, 0)
WHILE (@i > 0 AND (substring(@hexstr, @i, 1) like '[0-9A-Fa-f]'))
BEGIN
SET @hex = SUBSTRING(@hexstr, @i, 1)
SET @a = @a +
convert(bigint, CASE WHEN @hex LIKE '[0-9]'
THEN CAST(@hex as int)
ELSE CAST(ASCII(UPPER(@hex))-55 as int) end * @place)
set @place = @place * convert(bigint,16)
SET @i = @i - 1
END
RETURN convert(varbinary(8000),@a)
END
GO
<のhref = "http://sqlblog.com/blogs/peter_debetta/archive/2007/03/09/t-sql-convert-hex-string-to-varbinary.aspx" のrel = "nofollowをnoreferrer" >ソースの
所属していません StackOverflow