USE CanvasWarehouse GO IF OBJECT_ID(N'[dbo].[Canvas-v2-enrollment_terms-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-enrollment_terms-staging] END ; CREATE TABLE [dbo].[Canvas-v2-enrollment_terms-staging] ( id bigint NOT NULL , name varchar(max) NOT NULL , integration_id varchar(max) NOT NULL , created_at varchar(max) NOT NULL , updated_at varchar(max) NOT NULL , workflow_state varchar(max) NOT NULL , sis_batch_id bigint NULL , start_at varchar(max) NULL , end_at varchar(max) NULL , sis_source_id varchar(max) NOT NULL , term_code varchar(max) NOT NULL , grading_period_group_id bigint NULL , meta_ts varchar(max) NOT NULL ) GO BULK INSERT [dbo].[Canvas-v2-enrollment_terms-staging] FROM 'F:\Canvas\unpackedFiles\enrollment_terms.0.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-enrollment_terms]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-enrollment_terms] END ; CREATE TABLE [dbo].[Canvas-v2-enrollment_terms] ( id bigint NOT NULL , name varchar(256) NOT NULL , integration_id varchar(256) NOT NULL , created_at datetime NOT NULL , updated_at datetime NOT NULL , workflow_state varchar(max) NOT NULL , sis_batch_id bigint NULL , start_at datetime NULL , end_at datetime NULL , sis_source_id varchar(256) NOT NULL , term_code varchar(256) NOT NULL , grading_period_group_id bigint NULL , meta_ts datetime NOT NULL ) GO INSERT INTO [dbo].[Canvas-v2-enrollment_terms] SELECT id , name , integration_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 , sis_batch_id , CASE WHEN start_at IS NULL THEN NULL WHEN LEFT(start_at, 4) < 1900 THEN NULL ELSE CAST(start_at AS datetime) END as start_at , CASE WHEN end_at IS NULL THEN NULL WHEN LEFT(end_at, 4) < 1900 THEN NULL ELSE CAST(end_at AS datetime) END as end_at , sis_source_id , term_code , grading_period_group_id , 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-enrollment_terms-staging] IF OBJECT_ID(N'[dbo].[Canvas-v2-enrollment_terms-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-enrollment_terms-staging] END ;