USE CanvasWarehouse GO IF OBJECT_ID(N'[dbo].[Canvas-v2-scores-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-scores-staging] END ; CREATE TABLE [dbo].[Canvas-v2-scores-staging] ( id bigint NOT NULL , created_at varchar(max) NULL , updated_at varchar(max) NULL , workflow_state varchar(max) NOT NULL , assignment_group_id bigint NULL , enrollment_id bigint NOT NULL , grading_period_id bigint NULL , current_score REAL NULL , final_score REAL NULL , course_score varchar(max) NOT NULL , unposted_current_score REAL NULL , unposted_final_score REAL NULL , current_points REAL NULL , unposted_current_points REAL NULL , final_points REAL NULL , unposted_final_points REAL NULL , override_score REAL NULL , meta_ts varchar(max) NOT NULL ) GO BULK INSERT [dbo].[Canvas-v2-scores-staging] FROM 'F:\Canvas\unpackedFiles\scores.1.tsv' WITH ( FIRSTROW = 2 , FIELDTERMINATOR = '\t' --, ROWTERMINATOR = '\r\n' -- This is due to unix encoding, the row terminator is not \n , ROWTERMINATOR = '0x0A' , KEEPNULLS ) GO IF OBJECT_ID(N'[dbo].[Canvas-v2-scores]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-scores] END ; CREATE TABLE [dbo].[Canvas-v2-scores] ( id bigint NOT NULL , created_at datetime NULL , updated_at datetime NULL , workflow_state varchar(max) NOT NULL , assignment_group_id bigint NULL , enrollment_id bigint NOT NULL , grading_period_id bigint NULL , current_score REAL NULL , final_score REAL NULL , course_score bit NOT NULL , unposted_current_score REAL NULL , unposted_final_score REAL NULL , current_points REAL NULL , unposted_current_points REAL NULL , final_points REAL NULL , unposted_final_points REAL NULL , override_score REAL NULL , meta_ts datetime NOT NULL ) GO INSERT INTO [dbo].[Canvas-v2-scores] SELECT id , CASE WHEN created_at IS NULL THEN NULL WHEN LEFT(created_at, 4) < 1900 THEN NULL ELSE CAST(created_at AS datetime) END as created_at , CASE WHEN updated_at IS NULL THEN NULL WHEN LEFT(updated_at, 4) < 1900 THEN NULL ELSE CAST(updated_at AS datetime) END as updated_at , workflow_state , assignment_group_id , enrollment_id , grading_period_id , current_score , final_score , CAST( CASE WHEN course_score LIKE 'TRUE' THEN 1 WHEN course_score LIKE 'FALSE' THEN 0 ELSE NULL END as bit ) as course_score , unposted_current_score , unposted_final_score , current_points , unposted_current_points , final_points , unposted_final_points , override_score , CASE WHEN meta_ts IS NULL THEN NULL WHEN LEFT(meta_ts, 4) < 1900 THEN NULL ELSE CAST(REPLACE(REPLACE(REPLACE(meta_ts,CHAR(9),''),CHAR(10),''),CHAR(13),'') AS datetime) END as meta_ts FROM [dbo].[Canvas-v2-scores-staging] IF OBJECT_ID(N'[dbo].[Canvas-v2-scores-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-scores-staging] END ;