November 19, 2022 at 4:14 am
I am trying to convert some numbers pulled from a char field.
I would like to pull the 1 and 46 from the 000.001.070.000.0 to return as 1.46 .
I tried a couple of things but not quite hitting the mark.
create table Models (
Modelid integer,
Modelnameid char(17)
);
insert into Models values ( 1 , '000.001.070.000.0' );
select format(cast(SUBSTRING(Modelnameid , 10, 2) as numeric), 'x')
,cast(SUBSTRING(Modelnameid , 10, 2) as numeric)
,convert(varbinary(12),(cast(SUBSTRING(Modelnameid , 10, 2) as numeric)))
,Modelnameid
from Models;
November 19, 2022 at 1:05 pm
Why does this make sense? CONVERT using style 2 seems to convert to text and removes the leading '0x'
select Modelnameid origignal
,cast(SUBSTRING(Modelnameid , 10, 2) as numeric) substr_num
,convert(varbinary(12),(cast(SUBSTRING(Modelnameid , 10, 2) as numeric))) conv_vb
,cast(v.conv_to_vb as char(16)) cast_as_char
,v.conv_to_vb
,convert(char(16), v.conv_to_vb, 2) conv_vb_to_string
,stuff(substring(convert(char(16), v.conv_to_vb, 2), 8, 3), 2, 0, '.') stuff_conv_vb_to_string
from #Models
cross apply (values (convert(varbinary(12),(cast(SUBSTRING(Modelnameid , 10, 2) as numeric))))) v(conv_to_vb);
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
November 19, 2022 at 11:51 pm
I would like to pull the 1 and 46 from the 000.001.070.000.0 to return as 1.46 .
I'm totally lost here... where do you see a "46" in the "000.001.070.000.0" string? Are you saying that you want to convert the 070 to a hex 46?
Never mind... I figured it out...
--Jeff Moden
Change is inevitable... Change for the better is not.
November 20, 2022 at 12:52 am
Ok... when you have a ModelNameID like this...
000.001.002.000.0
... or like this...
000.160.170.000.0
... what do you want to return?
My thought is that it's a form of future computational suicide to drop the leading zeros on any hex conversion.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply