I imported a large amount of data using BCP into SQL Server that contained all strings. To keep further integrity of the db I am altering all the columns for there correct metadata.
I have a current string column that is actually full of decimal values and need to alter the column to make it decimal(p, s).
Because of the large amount of data, I wrote the following query to get the max length of the string before and after the decimal point; added the length of the string from before and after decimal to get precision. The following query will show what I arrived to:
SELECT
'('
+
CAST(MAX(LEN(LEFT(Measure, LEN(Measure) - LEN(SUBSTRING(Measure, CHARINDEX('.', Measure) , 255)))))
+
MAX(LEN(SUBSTRING(Measure, CHARINDEX('.', Measure) +1, 255))) AS varchar(5))
+
','
+ CAST(MAX(LEN(SUBSTRING(Measure, CHARINDEX('.', Measure) +1, 255))) AS varchar(5))
+
')' AS 'max[p,s]'
FROM
Result
The following is the result:
max[p,s]
--------
(24,19)
When I Alter the column by stating the following:
ALTER TABLE Result
ALTER COLUMN Measure DECIMAL (24,19)
I get the following error:
Msg 8115, Level 16, State 8, Line 1 Arithmetic overflow error converting varchar to data type numeric. The statement has been terminated.
(p,s) where p = precision and s=scale; I was under the impression that I can have a max precision (p) of 38. Why am I getting this error? Any suggestions? It is imperative that I don't lose any precision.