本文介紹了將十六進制轉換為 INT,反之亦然的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我將創建一個由十六進制值組成的序列號
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
現在要根據十六進制值創建序列號,我需要將 6 添加到最后生成的十六進制值
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.
我已經在 c# 中創建了它,但我需要在 SQL 上進行
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 中做到這一點?
How can this be done in T-SQL?
推薦答案
希望對你有幫助
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;
您可以根據答案創建視圖/過程/函數的基本成分,
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
這篇關于將十六進制轉換為 INT,反之亦然的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!