当我使用 FOR XMLfloat
从我的 SQL 数据库中获取类型值时,它会将它们格式化为科学格式,如下所示:
<foo bar="0.00000000e+000"/>
我更喜欢它说“0”。
我怎样才能说服它这样做?
当我使用 FOR XMLfloat
从我的 SQL 数据库中获取类型值时,它会将它们格式化为科学格式,如下所示:
<foo bar="0.00000000e+000"/>
我更喜欢它说“0”。
我怎样才能说服它这样做?
CAST是你的朋友
declare @t table (bar float); insert @t values (0);
select bar from @t for xml path('foo');
------------------------------------------------
| <foo><bar>0.000000000000000e+000</bar></foo> |
declare @t table (bar float); insert @t values (0);
select CAST(bar as decimal(10,2)) bar from @t for xml path('foo');
--------------------------------------------
| <foo><bar>0.00</bar></foo> |
declare @t table (bar float); insert @t values (0);
select CAST(bar as bigint) bar from @t for xml path('foo');
--------------------------------------------
| <foo><bar>0</bar></foo> |
使用我的功能,当需要兼容 Excel 或 SAS 等其他系统时使用 12 位精度,NB!它也有 ROUND
CREATE OR ALTER FUNCTION dbo.FormatAbacus( @p FLOAT(53))
RETURNS VARCHAR(40)
AS
BEGIN
DECLARE @dec INT = 10 -- may be as parameter in future
DECLARE @SIGN AS CHAR(1) = IIF( @p < 0 , '-', NULL);
IF @p IS NULL RETURN NULL
IF @p = 0 RETURN '0'
SET @p = ABS( @p);
DECLARE @exp AS INT = FLOOR(LOG10(@p)) ;
DECLARE @power AS FLOAT = POWER( CONVERT(FLOAT,10.0), @dec - @exp - 1);
DECLARE @mantis AS VARCHAR(40) = CONVERT( NUMERIC, ROUND( @p * @power,0));
DECLARE @ret AS VARCHAR(40) = CASE
WHEN 0 > @exp THEN CONCAT( LEFT( '0.00000000000000',1-@exp), @mantis)
WHEN @exp >= @dec THEN CONCAT( @mantis,LEFT( '000000000000000',@exp-@dec+1))
WHEN @exp = @dec - 1 THEN @mantis
ELSE STUFF( @mantis, @exp+2, 0, '.')
END;
IF @ret LIKE '%.%' SET @ret = LEFT( @ret, LEN(RTRIM(REPLACE(@ret,'0',' '))))
IF @ret LIKE '%.' SET @ret = REPLACE(@ret,'.','')
RETURN CONCAT( @SIGN, @ret)
END
跑:
SELECT
dbo.FormatAbacus(0.001234567890123456) AS '_0.001234567890123456'
, dbo.FormatAbacus( 0.1234567890123456) AS '_0.1234567890123456'
, dbo.FormatAbacus( 1.234567890123456) AS '_1.234567890123456'
, dbo.FormatAbacus( 123456.7890123456) AS '_123456.7890123456'
, dbo.FormatAbacus( 123456789012.3456) AS '_123456789012.3456'
, dbo.FormatAbacus( 12345678901234.56) AS '_12345678901234.56'
, dbo.FormatAbacus( 12345678901234560.) AS '_12345678901234560.'
, dbo.FormatAbacus( 123456789012345600.) AS '_123456789012345600.'
, dbo.FormatAbacus(NULL) AS '_NULL'
, dbo.FormatAbacus(0) AS '_0'
FOR XML PATH('Abacus_12_digits'),TYPE
产品:
<Abacus_12_digits>
<_0.001234567890123456>0.00123456789012 </_0.001234567890123456>
<_0.1234567890123456> 0.123456789012 </_0.1234567890123456>
<_1.234567890123456> 1.23456789012 </_1.234567890123456>
<_123456.7890123456> 123456.789012 </_123456.7890123456>
<_123456789012.3456> 123456789012 </_123456789012.3456>
<_12345678901234.56> 12345678901200 </_12345678901234.56>
<_12345678901234560.> 12345678901200000 </_12345678901234560.>
<_123456789012345600.> 123456789012000000</_123456789012345600.>
<!-- NULL is missing -->
<_0> 0 </_0>
</Abacus_12_digits>