Convert integer to hexa in sql
I want to convert int
to hexadecimal
in SQL.
Example:
SELECT CONVERT(VARBINARY(8), 162)
Result :0x000000A2
Actual value is this
A2
Why I am getting unnecessary part at prefix?
Can I remove previous part?
What is right way to handle it?
sql-server
add a comment |
I want to convert int
to hexadecimal
in SQL.
Example:
SELECT CONVERT(VARBINARY(8), 162)
Result :0x000000A2
Actual value is this
A2
Why I am getting unnecessary part at prefix?
Can I remove previous part?
What is right way to handle it?
sql-server
What unnecessary part? That value looks correct for avarbinary
to me
– Larnu
Nov 22 at 10:41
1
Possible duplicate of Convert integer to hex and hex to integer
– AlexK
Nov 22 at 10:50
add a comment |
I want to convert int
to hexadecimal
in SQL.
Example:
SELECT CONVERT(VARBINARY(8), 162)
Result :0x000000A2
Actual value is this
A2
Why I am getting unnecessary part at prefix?
Can I remove previous part?
What is right way to handle it?
sql-server
I want to convert int
to hexadecimal
in SQL.
Example:
SELECT CONVERT(VARBINARY(8), 162)
Result :0x000000A2
Actual value is this
A2
Why I am getting unnecessary part at prefix?
Can I remove previous part?
What is right way to handle it?
sql-server
sql-server
edited Nov 22 at 11:53
Birel
436113
436113
asked Nov 22 at 10:36
Pinky
64
64
What unnecessary part? That value looks correct for avarbinary
to me
– Larnu
Nov 22 at 10:41
1
Possible duplicate of Convert integer to hex and hex to integer
– AlexK
Nov 22 at 10:50
add a comment |
What unnecessary part? That value looks correct for avarbinary
to me
– Larnu
Nov 22 at 10:41
1
Possible duplicate of Convert integer to hex and hex to integer
– AlexK
Nov 22 at 10:50
What unnecessary part? That value looks correct for a
varbinary
to me– Larnu
Nov 22 at 10:41
What unnecessary part? That value looks correct for a
varbinary
to me– Larnu
Nov 22 at 10:41
1
1
Possible duplicate of Convert integer to hex and hex to integer
– AlexK
Nov 22 at 10:50
Possible duplicate of Convert integer to hex and hex to integer
– AlexK
Nov 22 at 10:50
add a comment |
3 Answers
3
active
oldest
votes
To quote the documentation:
When other data types are converted to binary or varbinary, the data is padded or truncated on the left. Padding is achieved by using hexadecimal zeros.
You're specifying VARBINARY(8)
in your query, so the result is padded with zeros to that length. If you need the value without the padding for some reason, specify VARBINARY(1)
, which will return 0xA2
.
Note: They're both the same value
Alternatively, if you just want a 2 character string:
SELECT RIGHT(CONVERT(VARCHAR(8),CONVERT(VARBINARY(8),162),2), 2)
Which will return A2
It work for me.PRINT CONVERT(VARCHAR(8),CONVERT(VARBINARY(1), 162),2)
– Pinky
Nov 22 at 11:27
1
@Pinky, okay, as long as you know the length of the significant figures
– Jodrell
Nov 22 at 11:36
add a comment |
At a complete a total guess in the absence of any response from the OP:
SELECT V.BloatedHex,
ISNULL(STUFF(V.BloatedHex,1,NULLIF(PATINDEX('%[^0]%',V.BloatedHex),0)-1,''),0)
FROM (VALUES(STUFF(CONVERT(varchar(10),CONVERT(varbinary(8),162),1),1,2,''))) V(BloatedHex);
This returns the varchar(10)
value 'A2'
.
add a comment |
You can do it in one statement like this,
DECLARE @someNumber BIGINT = 162;
WITH Hex AS (
SELECT CONVERT(VARCHAR(34), CONVERT(VARBINARY(8), @someNumber), 2) [Value]
)
SELECT SUBSTRING([Value], PATINDEX('%[^0]%', [Value]), 34) FROM Hex
;
This does not use any unsupported internal functions and attempts to minimize string manipulation.
Better still, don't write this kind of presentation code with TSQL, it is not what it is good at. Worry about making it look pretty when you display the value to the user.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429026%2fconvert-integer-to-hexa-in-sql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
To quote the documentation:
When other data types are converted to binary or varbinary, the data is padded or truncated on the left. Padding is achieved by using hexadecimal zeros.
You're specifying VARBINARY(8)
in your query, so the result is padded with zeros to that length. If you need the value without the padding for some reason, specify VARBINARY(1)
, which will return 0xA2
.
Note: They're both the same value
Alternatively, if you just want a 2 character string:
SELECT RIGHT(CONVERT(VARCHAR(8),CONVERT(VARBINARY(8),162),2), 2)
Which will return A2
It work for me.PRINT CONVERT(VARCHAR(8),CONVERT(VARBINARY(1), 162),2)
– Pinky
Nov 22 at 11:27
1
@Pinky, okay, as long as you know the length of the significant figures
– Jodrell
Nov 22 at 11:36
add a comment |
To quote the documentation:
When other data types are converted to binary or varbinary, the data is padded or truncated on the left. Padding is achieved by using hexadecimal zeros.
You're specifying VARBINARY(8)
in your query, so the result is padded with zeros to that length. If you need the value without the padding for some reason, specify VARBINARY(1)
, which will return 0xA2
.
Note: They're both the same value
Alternatively, if you just want a 2 character string:
SELECT RIGHT(CONVERT(VARCHAR(8),CONVERT(VARBINARY(8),162),2), 2)
Which will return A2
It work for me.PRINT CONVERT(VARCHAR(8),CONVERT(VARBINARY(1), 162),2)
– Pinky
Nov 22 at 11:27
1
@Pinky, okay, as long as you know the length of the significant figures
– Jodrell
Nov 22 at 11:36
add a comment |
To quote the documentation:
When other data types are converted to binary or varbinary, the data is padded or truncated on the left. Padding is achieved by using hexadecimal zeros.
You're specifying VARBINARY(8)
in your query, so the result is padded with zeros to that length. If you need the value without the padding for some reason, specify VARBINARY(1)
, which will return 0xA2
.
Note: They're both the same value
Alternatively, if you just want a 2 character string:
SELECT RIGHT(CONVERT(VARCHAR(8),CONVERT(VARBINARY(8),162),2), 2)
Which will return A2
To quote the documentation:
When other data types are converted to binary or varbinary, the data is padded or truncated on the left. Padding is achieved by using hexadecimal zeros.
You're specifying VARBINARY(8)
in your query, so the result is padded with zeros to that length. If you need the value without the padding for some reason, specify VARBINARY(1)
, which will return 0xA2
.
Note: They're both the same value
Alternatively, if you just want a 2 character string:
SELECT RIGHT(CONVERT(VARCHAR(8),CONVERT(VARBINARY(8),162),2), 2)
Which will return A2
edited Nov 22 at 11:22
answered Nov 22 at 10:43
Diado
1,37021015
1,37021015
It work for me.PRINT CONVERT(VARCHAR(8),CONVERT(VARBINARY(1), 162),2)
– Pinky
Nov 22 at 11:27
1
@Pinky, okay, as long as you know the length of the significant figures
– Jodrell
Nov 22 at 11:36
add a comment |
It work for me.PRINT CONVERT(VARCHAR(8),CONVERT(VARBINARY(1), 162),2)
– Pinky
Nov 22 at 11:27
1
@Pinky, okay, as long as you know the length of the significant figures
– Jodrell
Nov 22 at 11:36
It work for me.PRINT CONVERT(VARCHAR(8),CONVERT(VARBINARY(1), 162),2)
– Pinky
Nov 22 at 11:27
It work for me.PRINT CONVERT(VARCHAR(8),CONVERT(VARBINARY(1), 162),2)
– Pinky
Nov 22 at 11:27
1
1
@Pinky, okay, as long as you know the length of the significant figures
– Jodrell
Nov 22 at 11:36
@Pinky, okay, as long as you know the length of the significant figures
– Jodrell
Nov 22 at 11:36
add a comment |
At a complete a total guess in the absence of any response from the OP:
SELECT V.BloatedHex,
ISNULL(STUFF(V.BloatedHex,1,NULLIF(PATINDEX('%[^0]%',V.BloatedHex),0)-1,''),0)
FROM (VALUES(STUFF(CONVERT(varchar(10),CONVERT(varbinary(8),162),1),1,2,''))) V(BloatedHex);
This returns the varchar(10)
value 'A2'
.
add a comment |
At a complete a total guess in the absence of any response from the OP:
SELECT V.BloatedHex,
ISNULL(STUFF(V.BloatedHex,1,NULLIF(PATINDEX('%[^0]%',V.BloatedHex),0)-1,''),0)
FROM (VALUES(STUFF(CONVERT(varchar(10),CONVERT(varbinary(8),162),1),1,2,''))) V(BloatedHex);
This returns the varchar(10)
value 'A2'
.
add a comment |
At a complete a total guess in the absence of any response from the OP:
SELECT V.BloatedHex,
ISNULL(STUFF(V.BloatedHex,1,NULLIF(PATINDEX('%[^0]%',V.BloatedHex),0)-1,''),0)
FROM (VALUES(STUFF(CONVERT(varchar(10),CONVERT(varbinary(8),162),1),1,2,''))) V(BloatedHex);
This returns the varchar(10)
value 'A2'
.
At a complete a total guess in the absence of any response from the OP:
SELECT V.BloatedHex,
ISNULL(STUFF(V.BloatedHex,1,NULLIF(PATINDEX('%[^0]%',V.BloatedHex),0)-1,''),0)
FROM (VALUES(STUFF(CONVERT(varchar(10),CONVERT(varbinary(8),162),1),1,2,''))) V(BloatedHex);
This returns the varchar(10)
value 'A2'
.
answered Nov 22 at 10:50
Larnu
15.1k41530
15.1k41530
add a comment |
add a comment |
You can do it in one statement like this,
DECLARE @someNumber BIGINT = 162;
WITH Hex AS (
SELECT CONVERT(VARCHAR(34), CONVERT(VARBINARY(8), @someNumber), 2) [Value]
)
SELECT SUBSTRING([Value], PATINDEX('%[^0]%', [Value]), 34) FROM Hex
;
This does not use any unsupported internal functions and attempts to minimize string manipulation.
Better still, don't write this kind of presentation code with TSQL, it is not what it is good at. Worry about making it look pretty when you display the value to the user.
add a comment |
You can do it in one statement like this,
DECLARE @someNumber BIGINT = 162;
WITH Hex AS (
SELECT CONVERT(VARCHAR(34), CONVERT(VARBINARY(8), @someNumber), 2) [Value]
)
SELECT SUBSTRING([Value], PATINDEX('%[^0]%', [Value]), 34) FROM Hex
;
This does not use any unsupported internal functions and attempts to minimize string manipulation.
Better still, don't write this kind of presentation code with TSQL, it is not what it is good at. Worry about making it look pretty when you display the value to the user.
add a comment |
You can do it in one statement like this,
DECLARE @someNumber BIGINT = 162;
WITH Hex AS (
SELECT CONVERT(VARCHAR(34), CONVERT(VARBINARY(8), @someNumber), 2) [Value]
)
SELECT SUBSTRING([Value], PATINDEX('%[^0]%', [Value]), 34) FROM Hex
;
This does not use any unsupported internal functions and attempts to minimize string manipulation.
Better still, don't write this kind of presentation code with TSQL, it is not what it is good at. Worry about making it look pretty when you display the value to the user.
You can do it in one statement like this,
DECLARE @someNumber BIGINT = 162;
WITH Hex AS (
SELECT CONVERT(VARCHAR(34), CONVERT(VARBINARY(8), @someNumber), 2) [Value]
)
SELECT SUBSTRING([Value], PATINDEX('%[^0]%', [Value]), 34) FROM Hex
;
This does not use any unsupported internal functions and attempts to minimize string manipulation.
Better still, don't write this kind of presentation code with TSQL, it is not what it is good at. Worry about making it look pretty when you display the value to the user.
answered Nov 22 at 11:30
Jodrell
26.3k35794
26.3k35794
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429026%2fconvert-integer-to-hexa-in-sql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What unnecessary part? That value looks correct for a
varbinary
to me– Larnu
Nov 22 at 10:41
1
Possible duplicate of Convert integer to hex and hex to integer
– AlexK
Nov 22 at 10:50