Background
Just to give you come context I have included a little background as to why I'm trying to do this:
I have a table 'cp' which has an [id] field which is the identifier for each registration I have come realize that this is to be moved from a deployed solution to a cloud based as a result multiple accounts will be accessing the same objects and it would be best if they all start at 1 so rather than use the id field I have created an int field called label which will be used in conjunction with the account id to find the unique record per account.
field | type
________________________
id | int
label | int
account_id | int
When the registrations are printed out on the screen I want them to be displayed as a 5 digit HEX number and I can simply do the following in the c# code
value.ToString("X")
However My problem exists with the existing entries int he database before I added the new label field. I have found several questions with a similar but do not answer my question
- Converting a paragraph to hex notatation, then back to string
- Converting a String to HEX in SQL
- Convert integer to hex and hex to integer
The Problem
i have the fields with ids (1,2,3,----,11,12,13,---etc) values 1-9 can simply be copied over as the value.ToString("X") will simply do nothing with those values however if I simply copied value 11 would return "B" which correctly is the HEX value of 11 however I want it to return "11" so I need an SQL script which will convert 11 => 17 (which is the hex value of 0x11) so that when the application reads it it will output the value 11 to the screen.
This is only to occur once to convert the exiting id's which is why I'm wanting an SQL script to do it all in one batch rather than build it into the application.
What I would Like would be something like below so that it assumes the value in would be a hex value and convert it back to an Int.
SELECT CONVERT(INT, '0x' + id) FROM cp;
I can get the correct effect in C# by doing the following
int.Parse(value.ToString(),System.Globalization.NumberStyles.HexNumber).ToString()
UPDATE: I have found the solution to this problem for my use-case but some confusion has been raised that I am going to be doing all of my conversion in the database, I assume by the SELECT statement above. To clarify that this is a one-time event to convert existing records. Note the below statement does not work but stackoverflow will not let me edit the original statement as the change is less than 15 characters.
UPDATE [dbname].[dbo].[cp] set [Label] = CONVERT(INT, '0x' + id);