I have a table with 50,000+ records and need to copy all the records to another table which has the same columns(different data types) plus some extra columns. Now my requirement is there are two columns(in the destination table) which cannot have NULL values. Two columns and the data types in source and destination tables as following:
Source Table
Column Name Data Type
total_labor_hours nchar(10)
feet_produced nchar(10)
Destination Table
Column Name Data Type
total_labor_hrs Decimal(18,4)
labor_feet_produced Decimal(18,4)
Here is a sample dataset of the source table: http://www.sqlfiddle.com/#!18/f654e/1
Here is a sample dataset of the destination table: http://www.sqlfiddle.com/#!18/fe196d/1
Specifically the issue is if you see the destination table the Unique_ID = 2 record's "labor_feet_produced" column is NULL after copying. Which means the 0 in the source table has been converted to NULL when it's copied to destination table. I want it to be copied as 0.0000. I do not care about any NULL values in any other field but there shouldn't be any in "total_labor_hrs" & "labor_feet_produced" columns
I have tried two methods as following but did not succeed.
Method 1
When creating the destination table I unchecked "Allow Nulls" field for those two columns and tried copying but id wasn't successful.
These are the errors it gave me :
Copying to [dbo].[ALL_LABOR_DETAILS] (Error)
Messages
Error 0xc0202009: Data Flow Task 1: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80004005 Description: "Unspecified error".
(SQL Server Import and Export Wizard)
Error 0xc020901c: Data Flow Task 1: There was an error with Destination - ALL_LABOR_DETAILS.Inputs[Destination Input].Columns[feet_produced] on Destination - ALL_LABOR_DETAILS.Inputs[Destination Input]. The column status returned was: "The value violated the integrity constraints for the column.".
(SQL Server Import and Export Wizard)
Error 0xc0209029: Data Flow Task 1: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The "Destination - ALL_LABOR_DETAILS.Inputs[Destination Input]" failed because error code 0xC020907D occurred, and the error row disposition on "Destination - ALL_LABOR_DETAILS.Inputs[Destination Input]" specifies failure on error. An error occurred on the specified object of the specified component. There may be error messages posted before this with more information about the failure.
(SQL Server Import and Export Wizard)
Error 0xc0047022: Data Flow Task 1: SSIS Error Code DTS_E_PROCESSINPUTFAILED. The ProcessInput method on component "Destination - ALL_LABOR_DETAILS" (58) failed with error code 0xC0209029 while processing input "Destination Input" (71). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running. There may be error messages posted before this with more information about the failure.
(SQL Server Import and Export Wizard)
Error 0xc02020c4: Data Flow Task 1: The attempt to add a row to the Data Flow task buffer failed with error code 0xC0047020.
(SQL Server Import and Export Wizard)
Error 0xc0047038: Data Flow Task 1: SSIS Error Code DTS_E_PRIMEOUTPUTFAILED. The PrimeOutput method on Source - NOR_LABOR returned error code 0xC02020C4. The component returned a failure code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing. There may be error messages posted before this with more information about the failure.
(SQL Server Import and Export Wizard)
Method 2
Created the destination table without un-checking the "Allow Nulls" field for the two columns "total_labor_hrs" and "labor_feet_produced". Copying data was successful. But all the 0's were converted to NULL.
Again as mentioned above my goal is to copy any 0's in those two columns as 0.0000 without converting it to NULL.
How about just using try_convert()
?
select coalesce(try_convert(Decimal(18,4), total_labor_hours), 0)
My guess is that the space in the values are causing the problems. You should be using nvarchar()
rather than char()
.
Are the tables within the same database?
INSERT INTO [dbo].[ALL_LABOR_DETAILS] (
[ID]
,[trx_date]
,[work_order]
,[department]
,[work_center]
,[operation_no]
,[operator]
,[total_labor_hrs]
,[labor_feet_produced]
,[item_no]
)
SELECT
[ID]
,[trx_date]
,[work_order]
,[department]
,[work_center]
,[operation_no]
,[operator]
,[total_labor_hours]
,[feet_produced]
,[item_no]
--,[lot_no]
--,[default_bin]
--,[posted]
--,[labor_feet_produced]
FROM [dbo].[NOR_LABOR]
User contributions licensed under CC BY-SA 3.0