TSQL 将大数重新映射为小数,但保留身份

TSQL remap big number to smaller, but keep Identity(TSQL 将大数重新映射为小数,但保留身份)
本文介绍了TSQL 将大数重新映射为小数,但保留身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试将一个巨大的数字加载到 Field1 INT 中,它只能容纳 max=2,147,483,647,根据它我不能改变 DDL,所以试图找到临时解决方案,从这个数字的中间切出一位数,然后添加唯一性检查.
这些数字的格式如下:29000001234,所以我的意思是保持这种格式中间有零以便于识别.我不想在这个任务中引入任何新的列/表,因为那里的自由有限,这是 3rd 方模式.

I'm trying to load a huge number into Field1 INT which can hold only max=2,147,483,647, according to it I can't change DDL, so tried to find adhoc solution to cut out single digit from the middle of this number and then add check for uniqueness.
This numbers are in the format like: 29000001234, so I mean to keep this format with zeros in the middle to easy recognizing. I don't want to introduce any new columns/tables into this task, as limited in freedom there, this is 3rd party schema.

任何人都可以提出更好的解决方案,如何重新映射/保持所有数字低于该限制;这是我的草稿:

Can anybody suggest better solution, how to remap/keep all numbers under that limit; this is my draft:

DECLARE @fl FLOAT = 29000001234
DECLARE @I  INT

SELECT  @i = (SUBSTRING(CAST(CAST(@fl AS BIGINT) AS VARCHAR(18)),1,4) +  
          SUBSTRING(CAST(CAST(@fl AS BIGINT) AS VARCHAR(18)),7,LEN(CAST(CAST(@fl AS BIGINT) AS VARCHAR(18))))  )
select @i;

推荐答案

如果没有算术溢出或丢失原始数据,您就无法做到这一点.
如果目标表或查询的列数有限制,请使用多行:

You can't do that without any arithmetic overflow, or with out losing your original data.
If you have a limitation in columns of your destination table or query, use multiple rows:

declare @c bigint = 29000001234;
declare @s bigint = 1000000000;    -- Separator value

;with cte(partNo, partValue) as (
    select 1, @c % @s
    union all
    select partNo + 1, (@c / power(@s, partNo)) % @s
    from cte
    where (@c / power(@s, partNo)) > 0
)
select partValue
from cte;

这篇关于TSQL 将大数重新映射为小数,但保留身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Error casting varchar containing a number expressed in scientific notation to decimal(将包含以科学记数法表示的数字的 varchar 转换为十进制时出错)
Calculate cumulative product value(计算累积产品价值)
SQL Server loop for changing multiple values of different users(用于更改不同用户的多个值的 SQL Server 循环)
SQL Server 2008: Can a multi-statement UDF return a UDT?(SQL Server 2008:多语句 UDF 能否返回 UDT?)
Merge columns in sql(合并sql中的列)
SQL Column is invalid in ORDER BY, not contained in aggregate or GROUP BY(SQL 列在 ORDER BY 中无效,未包含在聚合或 GROUP BY 中)