I have an existing table full of data that can be created using
CREATE TABLE __EpisodeCost
(
ActivityRecordID INT NOT NULL,
ActCstID NVARCHAR(15),
VolAmt FLOAT,
ActCnt FLOAT,
TotCst FLOAT,
ResCstID NVARCHAR(50)
);
This comes from a feed that I have no control over, and I want to convert it to my own version called EpisodeCost
CREATE TABLE EpisodeCostCtp
(
ActivityRecordID INT NOT NULL,
ActCstID NVARCHAR(6),
ResCstID NVARCHAR(7),
ActCnt NVARCHAR(7),
TotCst DECIMAL(18, 8)
);
Now I have a problem with conversions. I can fulfill the request
SELECT
ActivityRecordID,
Cast(ActCstID AS NVARCHAR(6)),
Cast(ResCstID AS NVARCHAR(7)),
Cast(LTRIM(STR(ActCnt, 10)) AS NVARCHAR(7)),
Cast(TotCst AS DECIMAL(18, 8))
FROM __EpisodeCostCtp;
and it provides data, however, when I try to execute
INSERT INTO EpisodeCostCtp
(
ActivityRecordID,
ActCstID,
ResCstID,
ActCnt,
TotCst
)
SELECT
ActivityRecordID,
Cast(ActCstID AS NVARCHAR(6)),
Cast(ResCstID AS NVARCHAR(7)),
Cast(LTRIM(STR(ActCnt, 10)) AS NVARCHAR(7)),
Cast(TotCst AS DECIMAL(18, 8))
FROM __EpisodeCostCtp;
I get
Msg 8115, Level 16, State 8, Line 102 An arithmetic overflow error converting numeric data to numeric data. Application completed.
Why can I SELECTuse the appropriate roles but cannot INSERTin the target table?
Change I still don’t know what is going on here.
Following Serg's recommendations, I tried to find the problematic entries, but the query
SELECT
ActivityRecordID,
Cast(ActCstID AS NVARCHAR(6)),
Cast(ResCstID AS NVARCHAR(7)),
Cast(LTRIM(STR(ActCnt, 10)) AS NVARCHAR(7)),
Cast(TotCst AS DECIMAL(18, 8))
FROM __EpisodeCostCtp
WHERE TotCst > 9.999999999999999e9;
. 9.999999999999999e8 , / . scince INSERT DECIMAL(36, 18), , . , , SELECT , INSERT , .