Exporting SQL Server 2005 data to CSV:  Decimal values inaccurate (such as 4.000000000001 rather than just 4)

Posted on 2011-05-04
Last Modified: 2012-05-11
I am trying to export a view from SQL Server 2005 to a CSV file.  One of the fields, PriceBreakControllingUnitPrice, is a float data type, which contains price information that is almost always 2 decimal places.  When I execute the view, SQL Server Management Studio is showing no more than 2 decimal places.  When this is exported to CSV, a value such as 8.65 becomes 8.6500000000000004 or 4.84 becomes 4.8399999999999999 .  How do I fix this?

I have made multiple attempts at different CAST and CONVERT functions, but it has no effect.

I run into this problem with Access databases on occasion, and rounding functions always take care of the issue, but not this time with SQL Server.

Below is the SELECT statement without any conversion added, but this info probably is not really needed for this question.

SELECT     SubQuery1.PriceBook, SubQuery1.CustomerID, dbo.FS_CustomerItem.CustomerItemNumber, dbo.FS_PriceBreak.PriceBreakControllingUnitPrice,
                      dbo.FS_PriceBreak.PriceBreakQuantityFrom, dbo.FS_PriceBreak.PriceBreakQuantityTo, dbo.FS_PriceBreak.PriceBreakPriceBasis
FROM         (SELECT     PriceBook, CASE WHEN [PriceBook] = 'XX 10' THEN '123456' END AS CustomerID, PriceBookItemNumber, ItemKey,
                       FROM          dbo.FS_PriceBookItem
                       WHERE      (PriceBook IN ('XX 10'))) AS SubQuery1 INNER JOIN
                      dbo.FS_CustomerItem ON SubQuery1.CustomerID = dbo.FS_CustomerItem.CustomerID AND
                      SubQuery1.ItemKey = dbo.FS_CustomerItem.ItemKey INNER JOIN
                      dbo.FS_PriceBreak ON SubQuery1.PriceBookItemKey = dbo.FS_PriceBreak.PriceBookItemKey
Question by:HomerTNachoCheese
    LVL 6

    Author Comment

    For cast and convert, I have tried to convert to decimal(20,2), decimal(12,2), decimal(12,4) - all of which have no effect.  I also tried casting as integer, multiplying by 1000, casting that result as decimal(12,4), and dividing by 1000 - still did not work.

    I will try some other data formats.  Resulting file must be a CSV file.
    LVL 6

    Author Comment

    using numeric rather than decimal appears to work in testing.  Now I will try to create the SSIS package and make sure all is still well.  Also used .txt format instead for the test, which should also be an OK substitute for CSV (I doubt it matters to SQL Server).
    LVL 6

    Accepted Solution

    CSV vs TXT for the output file format actually makes a difference.  If I use CSV, I get extra decimal places and inaccurate numbers.  If I use TXT, then I get the expected results.
    LVL 6

    Author Closing Comment

    I figured it out.  The problem is not what I thought - conversion was probably not the issue at all, but instead it was a bug (cough... feature) of Microsoft SQL Server 2005.  Maybe there is a reason why exporting to CSV or TXT gives two different results.  Since the database that needs this data will accept either TXT or CSV, this is OK for me.  

    If someone else reading this has the same problem and requires CSV, then maybe the data can be first exported to TXT then renamed to CSV.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
    Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now