I have a table with doubles like 0.681672875510799
so for example a dummy table:
a1 a2
-------------------------
1 0.681672875510799
NULL 1
NULL NULL
NULL NULL
NULL NULL
NULL NULL
When I do
DECLARE @CommaString nvarchar(max)
SET @CommaString = ''
SELECT @CommaString =
STUFF(
(SELECT ',' + CAST([col] AS VARCHAR) FROM (
SELECT [a1] AS col FROM [ta] UNION ALL
SELECT [a2] AS col FROM [ta]
) alldata FOR XML PATH('') ) , 1 , 1 , '' )
PRINT @CommaString;
This prints:
1,0.681673,1
so I am losing several decimals, which are also important, How do I modify the code to get
1,0.681672875510799,1 instead of 1,0.681673,1?
In your inner query:
Casting the FLOAT to a DECIMAL works for me (SQL Server 2008 R2). You may have to tweak the (18,15) to work with your data.
Just noticed one more thing that works (and probably more consistently):