Convert Hexadecimal to INT and vice versa(将十六进制转换为 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,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将十六进制转换为 INT,反之亦然
基础教程推荐
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01