Convert integer to hexa in sql
up vote
-1
down vote
favorite
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 |
up vote
-1
down vote
favorite
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 |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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
404111
404111
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
up vote
1
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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 |
up vote
1
down vote
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 |
up vote
1
down vote
up vote
1
down vote
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,31621015
1,31621015
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 |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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
14.4k31530
14.4k31530
add a comment |
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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.2k35694
26.2k35694
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