[英]Remove trailing 0 when CAST as NVarChar
I have data as Decimal(15,4)
and values can be like 14.0100
, or 14.0000
, or 14.9999
我的数据为
Decimal(15,4)
并且值可以是14.0100
,或14.0000
,或14.9999
For integration with other system we have to store this kind of data in NVarChar(MAX)
attributes table.为了与其他系统集成,我们必须将此类数据存储在
NVarChar(MAX)
属性表中。 When I run CAST(Field AS NVarChar(MAX))
I get string values like 0.0000
当我运行
CAST(Field AS NVarChar(MAX))
我得到像0.0000
这样的字符串值
What I want is to trim trailing zeros (and period if needed) from those strings because data later used in online transmission and it's much better to send 14
instead of 14.0000
我想要的是从这些字符串中修剪尾随零(如果需要,还有句点),因为数据稍后用于在线传输,发送
14
而不是14.0000
How do I do that?我怎么做?
SQL Server 2012+
you could use FORMAT
, with SQL Server 2008
you could use string manipulation: SQL Server 2012+
可以使用FORMAT
, SQL Server 2008
可以使用字符串操作:
CREATE TABLE #tab(col DECIMAL(15,4));
INSERT INTO #tab(col)
VALUES (14.0100), (14.0000),
(14.9999), (10), (0),
(-1), (-10), (-12.01), (-12.10);
SELECT
col
,result_2012 = FORMAT(col, '########.####')
,result_2008 = CASE
WHEN col = 0 THEN '0'
ELSE LEFT(col,LEN(col) -
CASE WHEN PATINDEX('%[1-9]%', REVERSE(col)) < PATINDEX('%[.]%', REVERSE(col))
THEN PATINDEX('%[1-9]%', REVERSE(col)) - 1
ELSE PATINDEX('%[.]%', REVERSE(col))
END)
END
FROM #tab;
You could try casting the Decimal numbers to floats before casting then casting them to NVarChars ie CAST(CAST(Field as Float(8)) as NVarChar(MAX)).
您可以尝试在转换之前将 Decimal 数字转换为浮点数,然后将它们转换为
NVarChars ie CAST(CAST(Field as Float(8)) as NVarChar(MAX)).
The only issue you might have with this is if any of your existing numbers have a greater precision than an 8 byte float can deal with which should be easy enough to check for.您可能遇到的唯一问题是,您现有的任何数字是否具有比 8 字节浮点数可以处理的精度更高的精度,这应该很容易检查。
It would also be possible to remove the extra zeros after you've casted the numbers as strings using case statements, ie在使用 case 语句将数字转换为字符串后,也可以删除多余的零,即
case when right(Field, 4) = '0000' then
left(Field, len(Field) - 5) -- -5 to remove the decimal point
else case when right(Field, 3) = '000' Then
left(Field, len(Field) - 3)
else case when right(Field, 2) = '00' Then
left(Field, len(Field) - 2)
else case when right(Field, 1) = '0' Then
left(Field, len(Field) - 1)
end end end end
--With a lot of fantasy
--Tip: to optimize performances, first select values in a @-table, then do this transformation.
select
left( cast(Field as varchar(max)), charindex('.', cast(Field as varchar(max))) - 1) +
Case when reverse(cast(cast(reverse(substring(cast(Field as varchar), charindex('.', cast(Field as varchar)) + 1 ,4)) as int) as nvarchar)) = 0 Then '' Else
'.' + reverse(cast(cast(reverse(substring(cast(Field as varchar), charindex('.', cast(Field as varchar)) + 1 ,4)) as int) as nvarchar)) End
simple casting to REAL then back to varch(max) if you want will remove the leading 0s as follows:如果需要,简单地转换为 REAL 然后返回到 varch(max) 将删除前导 0,如下所示:
declare @a as Decimal(15,4)=14.0010
select cast(cast (@a as real) as varchar(max)) as a2
-- ouptut is 14.001
declare @a as Decimal(15,4)=14.0000
select cast(cast (@a as real) as varchar(max)) as a2
-- output is 14
The Idea here is to use the built in string function of Trimming spaces to TRIM ZEROs from the converted decimal as follows:这里的想法是使用 Trimming space 的内置字符串函数从转换后的十进制中修剪零,如下所示:
declare @d as Decimal(15,4)=11400012.00000, @s0 VARCHAR(MAX), @s VARCHAR(MAX)
set @s0=cast(@d as varchar(max))
set @s=left(@s0,len(rtrim(replace(@s0,'0',' '))))
if right(@s,1)='.' set @s=left(@s,len(@s)-1)
select @s
-- returns 11400012.001
You also can define the function to do this task for you:您还可以定义函数来为您完成此任务:
CREATE FUNCTION TRIM0(@s0 varchar(max))
RETURNS varchar(max)
AS
BEGIN
declare @s varchar(max)=''
set @s=left(@s0,len(rtrim(replace(@s0,'0',' '))))
if right(@s,1)='.' set @s=left(@s,len(@s)-1)
return @s
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.