本文介紹了將十六進(jìn)制轉(zhuǎn)換為 INT,反之亦然的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我將創(chuàng)建一個(gè)由十六進(jìn)制值組成的序列號(hào)
I will be creating a sequential Serial Number made from Hexadecimal values
使用這種格式:
XX-XX-XX-YYYY
Which XX-XX-XX is default value
And YYYY is the incrementing hexa decimal value
現(xiàn)在要根據(jù)十六進(jìn)制值創(chuàng)建序列號(hào),我需要將 6 添加到最后生成的十六進(jìn)制值
Now to create the serial number based on hex value I need Add 6 to the last generated hex value
MIN: 2D41 + 6 = 2D47
2D47 + 6 ... and so on
MAX: 4100 generation of serial will stop when I meet the MAX value.
我已經(jīng)在 c# 中創(chuàng)建了它,但我需要在 SQL 上進(jìn)行
I already created it in c# but I need to do it on SQL
int num1 = int.Parse("2D41", NumberStyles.HexNumber); //Convert hex to int
int result = num1 + 6; //Add + 6 for increment
string myHex = result.ToString("X"); //Convert result to hex
MessageBox.Show(myHex); // result 2D47
如何在 T-SQL 中做到這一點(diǎn)?
How can this be done in T-SQL?
推薦答案
希望對(duì)你有幫助
declare @seed varchar(max) = '2D41';
declare @limit varchar(max) = '4100';
select convert(int, convert(varbinary(max), '0x'+@seed,1)),
convert(int, convert(varbinary(max), '0x'+@limit,1));
;with seedlimit(seed, limit) as (
select convert(int, convert(varbinary(max), '0x'+@seed,1)),
convert(int, convert(varbinary(max), '0x'+@limit,1))
)
select SerialNumber = 'XX-XX-XX-' + right(convert(varchar(10),cast(s.seed + 6 * v.number as varbinary(max)),1),4)
from seedlimit s
join master.dbo.spt_values v on type='p'
where s.seed + 6 * v.number <= s.limit;
您可以根據(jù)答案創(chuàng)建視圖/過程/函數(shù)的基本成分,
The basic ingredients are in there for you to create a view/procedure/function out of the answer,
輸出:
SerialNumber
-------------
XX-XX-XX-2D41
XX-XX-XX-2D47
...
XX-XX-XX-40F7
XX-XX-XX-40FD
這篇關(guān)于將十六進(jìn)制轉(zhuǎn)換為 INT,反之亦然的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!