[英]Add Trailing Zeroes After Decimal
I am working with sku numbers that have the following 9 character structure:我正在使用具有以下 9 个字符结构的 sku 编号:
a.一种。 a 3 digit number,
一个 3 位数字,
b.湾a period,
一段时间,
c. C。 a five digit number.
一个五位数。
An example: 505.12345.一个例子:505.12345。
A considerable % of the sku's end in 0. Examples: 505.12340, 505.12300, 505.12000. sku 的很大一部分以 0 结尾。例如:505.12340、505.12300、505.12000。
I had no trouble keeping the trailing zeroes in SQL Server by setting the datatype to varchar after the migration from S3 -> SQL Server.从 S3 -> SQL Server 迁移后,通过将数据类型设置为 varchar,我可以轻松地在 SQL Server 中保留尾随零。 I used a new machine learning model in AWS Sagemaker that cut off the trailing zeroes prior to the migration to S3.
我在 AWS Sagemaker 中使用了一种新的机器学习模型,该模型在迁移到 S3 之前切断了尾随零。
The example sku's above now look like: 505.1234, 505.123, 505.12上面的示例 sku 现在看起来像:505.1234、505.123、505.12
My question: what is the best way to add trailing zeroes to all sku's where LEN([sku]) < 9?我的问题:将尾随零添加到 LEN([sku]) < 9 的所有 sku 的最佳方法是什么? I would prefer to keep the sku datatype as varchar.
我更愿意将 sku 数据类型保留为 varchar。
If you have a string, you can right-pad it with 0
s as follows:如果你有一个字符串,你可以用
0
s 右填充它,如下所示:
left(sku + replicate('0', 9), 9)
Alternatively:或者:
sku + replicate('0', 9 - len(sku))
Demo on DB Fiddle : DB Fiddle 上的演示:
select sku,
left(sku + replicate('0', 9), 9) new_sku,
sku + replicate('0', 9 - len(sku)) new_sku2
from (values ('505.1234'), ('505.123'), ('505.12'), ('505.12345')) x(sku)
sku | new_sku | new_sku2 :-------- | :-------- | :-------- 505.1234 | 505.12340 | 505.12340 505.123 | 505.12300 | 505.12300 505.12 | 505.12000 | 505.12000 505.12345 | 505.12345 | 505.12345
One simple way would be to CAST
back to DECIMAL(9, 5) and then CAST again to CHAR(9)一种简单的方法是
CAST
回 DECIMAL(9, 5) 然后再次 CAST 到 CHAR(9)
Data数据
drop table if exists #tTable;
go
create table #tTable(
dec_num varchar(20) not null);
Insert into #tTable values
('505.1234'),
('505.123'),
('505.12');
Query询问
select cast(cast(dec_num as decimal(9,5)) as char(9)) char_9
from #tTable;
Output输出
char_9
505.12340
505.12300
505.12000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.