August 31, 2018 at 12:32 pm
my original raw csv file contains column of "integer" but like String:
ID, Account, Amount
1,234,"56,789"
2,345,"6,789.01"
This is actually common in English denoting numbering with comma, however, because this is a csv file, meaning a comma could introduce that column be split into two columns, hence there is a double quote pair around it.
How could I make the right cast to convert those values into decimal or integer correctly?
Can anyone help? Thank you very much
August 31, 2018 at 12:36 pm
select convert(decimal(10,2), replace(replace('"6,789.01"', '"', ''), ',',''))
would do the trick - but if using SSIS or other tools those sometimes handle that on their own without any particular coding.
August 31, 2018 at 12:44 pm
If you're using an ETL tool, then simply specifying your text qualifier as " with handle the quotations (and cause the ETL tool to ignore them during the import". Frederico is right as well, many ETL tools will be happy to implicitly convert a value like 123,456,789.123 to 123456789.123. If it isn't, however, then the easiest way would be to import the value as text and use a transformation to remove the commas (REPLACE is a very common function, with the same usage across many languages), and then cast/convert it to a decimal.
I don't think you need to do it, but if you were using SSIS, and needed to both remove the commas and convert it, the syntax would be something like:(DT_NUMERIC,12,3)(REPLACE([Amount],",",""))
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
August 31, 2018 at 12:48 pm
frederico_fonseca - Friday, August 31, 2018 12:36 PMselect convert(decimal(10,2), replace(replace('"6,789.01"', '"', ''), ',',''))
would do the trick - but if using SSIS or other tools those sometimes handle that on their own without any particular coding.
Thank you very much for your quick help.
August 31, 2018 at 12:51 pm
Thom A - Friday, August 31, 2018 12:44 PMIf you're using an ETL tool, then simply specifying your text qualifier as " with handle the quotations (and cause the ETL tool to ignore them during the import". Frederico is right as well, many ETL tools will be happy to implicitly convert a value like 123,456,789.123 to 123456789.123. If it isn't, however, then the easiest way would be to import the value as text and use a transformation to remove the commas (REPLACE is a very common function, with the same usage across many languages), and then cast/convert it to a decimal.I don't think you need to do it, but if you were using SSIS, and needed to both remove the commas and convert it, the syntax would be something like:
(DT_NUMERIC,12,3)(REPLACE([Amount],",",""))
Thank you, indeed this is not used in SSIS.
August 31, 2018 at 8:38 pm
Instead of using expensive REPLACEs, you could always convert it to the MONEY datatype and then to DECIMAL. The MONEY datatype takes commas in stride.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 20, 2022 at 7:10 am
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply