Raw cast of arbitrary string to integer I have non-numeric string IDs in a table, and would like to cast them to unique integers. I need the mapping to be invertible / one-to-one. The string IDs are of uniform length.

I want to do this by literally aggregating the ASCII codes of each character in the ID. The ASCII function only returns the leftmost character. Is there an efficient way to cast the entire string?
Presuming you are using SQL Server, which you still haven't confirmed, I'd use an inline table-valued-function (TVF) that can be used to efficiently and safely convert character-data into an integer value, for strings of 8 characters or less.  The string must be a maximum of 8 characters in length since that is the maximum number of bytes for an integer in SQL Server.  

This is the TVF:

IF OBJECT_ID('dbo.GetBigInt') IS NOT NULL
DROP FUNCTION dbo.GetBigInt;
GO
CREATE FUNCTION dbo.GetBigInt (@data VARCHAR(1000))
RETURNS TABLE
WITH SCHEMABINDING
AS RETURN
SELECT BigIntResult = CASE WHEN LEN(@data) &gt; 8 THEN 1/0 
    ELSE CONVERT(BIGINT, CONVERT(VARBINARY(8), @data, 0)) END;
GO


To test the function, I created the following test-bed table with some sample rows:

IF OBJECT_ID('tempdb..#t') IS NOT NULL
DROP TABLE #t;
CREATE TABLE #t
(
    SomeText VARCHAR(100)
);

INSERT INTO #t (SomeText)
VALUES ('aaaaaaaa')
    , ('AAAAAAAA')
    , ('aaaaaaab')
    , ('ZZZZZZZZ')
    , ('ÿÿÿÿÿÿÿÿ')
    , (CHAR(1) + CHAR(1) + CHAR(1) + CHAR(1) + CHAR(1) + CHAR(1) + CHAR(1) + CHAR(1))
    , (CHAR(127) + CHAR(127) + CHAR(127) + CHAR(127) + CHAR(127) + CHAR(127) + CHAR(127) + CHAR(127))
    , (CHAR(128) + CHAR(127) + CHAR(128) + CHAR(128) + CHAR(128) + CHAR(128) + CHAR(128) + CHAR(128))
    , ('aaaaaaaba');


Run the TVF against the table like so:

SELECT *
FROM #t t
CROSS APPLY dbo.GetBigInt(t.SomeText) g;


The last row in the sample intentionally has 9 characters, which cannot be reliably converted into an 8-byte integer.  When the TVF attempts to convert that row, it will display the following error:


  Msg 8134, Level 16, State 1, Line 32
  Divide by zero error encountered.


The results for the other rows in the sample data are:



To test the speed of the TVF, I inserted 1,000,000 rows into the table, like so:

INSERT INTO #t (SomeText)
SELECT TOP(1000000) LEFT(c1.name, 8)
FROM sys.columns c1
    CROSS JOIN sys.columns c2
    CROSS JOIN sys.columns c3;


The execution plan for the query running against 1,000,000 rows:



If your "non-numeric string IDs" are longer than 8 bytes, I'd recommend simply converting them to BINARY(x) or VARBINARY(x) values, where x is then maximum length of your character data.

IF OBJECT_ID('dbo.GetBinary') IS NOT NULL
DROP FUNCTION dbo.GetBinary;
GO
CREATE FUNCTION dbo.GetBinary (@data VARCHAR(1000))
RETURNS TABLE
WITH SCHEMABINDING
AS RETURN
SELECT BinaryResult = CONVERT(VARBINARY(1000), @data, 0);
GO

SELECT *
FROM #t t
CROSS APPLY dbo.GetBinary(t.SomeText) g;