Hi all,
I am looking for a select statement that will compare the alphanumeric value of my input string (Char(10)) and return the closest matching alphanumeric value from my table (also Char(10)).
If my value to compare was numeric - I would use something like this:
SELECT TOP 1 * FROM [myTable] ORDER BY ABS( Val - @input )
However, because it is a Character data type I think I need to convert my character string to the value SQL Server uses to sort Character data types.
Here is some test code:
USE tempdb; IF OBJECT_ID('Bins', 'U') IS NOT NULL DROP TABLE Bins; GO CREATE TABLE Bins ( ID int identity primary key, BinCode char(10) ) INSERT INTO Bins VALUES ('ABCDEFHI8'); INSERT INTO Bins VALUES ('ABCDEFHI1') INSERT INTO Bins VALUES ('ABCDEFHI4') INSERT INTO Bins VALUES ('ABCDEFHI9') INSERT INTO Bins VALUES ('ABCDEFHI2') DECLARE @Bin CHAR(10) = 'ABCDEFHI3'
I need a Select statement that will return either record ABCDEFHI4 or ABCDEFHI2 i.e. they're both equal in 'alphanumeric distance'. Note the last character will not guaranteed to be numeric.
Thanks for your assistance.
Clay