Convert decimal to hexadecimal for each record in SQL query

Hi Experts,
I have a basic SQL select query and one of the Column names is "docid" which is a decimal number field. I would like to be able to convert this decimal number for each record into hexadecimal by incorporating within the query & inserting the hexadecimal value for each record into a new column each time the query is run.

Is this possible?

select d.docid, d.name, r.date, u.Username
FROM docs d
      join rev r on d.DocID = r.DocID
      join users u on r.userid = u.userid

Thanks
sherlock1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
select d.docid, d.name, r.date, u.Username, cast (d.docid as varbinary(8)) as hexID
FROM docs d
      join rev r on d.DocID = r.DocID 
      join users u on r.userid = u.userid

Open in new window

if you want to have the value temporary only, as 8 bytes (16 hex digits) in hex.
You can also add a computed column to the table definition with the same cast to always have the hex values available.
0
sherlock1Author Commented:
Thanks for your reply, that works thanks although if possible i would like just the last  number of the hex displayed (eg "5" or "E" ) if this is possible? (hopefully with not much change needed?

I guess I should of specified this - sorry
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
In that case use
select d.docid, d.name, r.date, u.Username, substring('0123456789ABCDEF', d.docid % 16 +1,1) as hexID
FROM docs d
      join rev r on d.DocID = r.DocID 
      join users u on r.userid = u.userid

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

sherlock1Author Commented:
ok that's great thanks, I guess the 0123456789ABCDEF part fomats the chracters
and % 16 +1,1 says there is 16 potential characters + 1?
could you explain a little on what this is doing please?

Thanks
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
x % 16   generates 0-15 (remainder of a integer division by 16).
+1, as we need it to be 1-16
The substring then takes a single character of '0-9A-F' at the corresponding position, basically translating it to a single hex digit.
0
sherlock1Author Commented:
ok, thanks for clarifying this with the additional comments
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.