USE CanvasWarehouse GO IF OBJECT_ID(N'[dbo].[Canvas-v2-accounts-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-accounts-staging] END ; CREATE TABLE [dbo].[Canvas-v2-accounts-staging] ( id bigint NOT NULL , name varchar(256) NULL , deleted_at varchar(max) NULL , parent_account_id bigint NULL , current_sis_batch_id bigint NULL , storage_quota bigint NULL , default_storage_quota bigint NULL , default_locale varchar(256) NULL , default_user_storage_quota bigint NULL , default_group_storage_quota bigint NULL , integration_id varchar(256) NULL , lti_context_id varchar(256) NULL , consortium_parent_account_id bigint NULL , course_template_id bigint NULL , created_at varchar(max) NOT NULL , updated_at varchar(max) NOT NULL , workflow_state varchar(max) NOT NULL , default_time_zone varchar(max) NOT NULL , uuid varchar(256) NULL , sis_source_id varchar(256) NULL , meta_ts varchar(max) NOT NULL ) GO BULK INSERT [dbo].[Canvas-v2-accounts-staging] FROM 'F:\Canvas\unpackedFiles\accounts.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-accounts]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-accounts] END ; CREATE TABLE [dbo].[Canvas-v2-accounts] ( id bigint NOT NULL , name varchar(256) NULL , deleted_at datetime NULL , parent_account_id bigint NULL , current_sis_batch_id bigint NULL , storage_quota bigint NULL , default_storage_quota varchar(256) NULL , default_locale varchar(256) NULL , default_user_storage_quota bigint NULL , default_group_storage_quota bigint NULL , integration_id varchar(256) NULL , lti_context_id varchar(256) NULL , consortium_parent_account_id bigint NULL , course_template_id bigint NULL , created_at datetime NOT NULL , updated_at datetime NOT NULL , workflow_state varchar(256) NULL , default_time_zone varchar(256) NULL , uuid varchar(256) NULL , sis_source_id varchar(256) NULL , meta_ts datetime NOT NULL ) GO INSERT INTO [dbo].[Canvas-v2-accounts] SELECT id , name , CASE WHEN deleted_at IS NULL THEN NULL WHEN LEFT(deleted_at, 4) < 1900 THEN NULL ELSE CAST(deleted_at AS datetime) END as deleted_at , parent_account_id , current_sis_batch_id , storage_quota , default_storage_quota , default_locale , default_user_storage_quota , default_group_storage_quota , integration_id , lti_context_id , consortium_parent_account_id , course_template_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 , default_time_zone , uuid , sis_source_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-accounts-staging] IF OBJECT_ID(N'[dbo].[Canvas-v2-accounts-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-accounts-staging] END ;