Archive for December, 2011

Convert an 8 character string to a 64-bit integer

You can store an 8 character (10 byte) string as an integer in SQL Server. Note that any more than 8 characters will get truncated.

I mean, you probably shouldn’t ever do it, but it works. Here’s how:

Convert String to Int

SELECT CONVERT(BIGINT, CONVERT(VARBINARY, 'ABC123DE'))

yields:

4702394839502898245

Convert Int back to string

SELECT CONVERT(VARCHAR(8), CONVERT(VARBINARY, CONVERT(BIGINT, 4702394839502898245)))

Yields:

ABC123DE

Convert String to Int – then back to a String again:

SELECT CONVERT(VARCHAR(8), CONVERT(VARBINARY, CONVERT(BIGINT, CONVERT(VARBINARY, 'ABC123DE'))))