USE CanvasWarehouse GO IF OBJECT_ID(N'[dbo].[Canvas-v2-account_users-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-account_users-staging] END ; CREATE TABLE [dbo].[Canvas-v2-account_users-staging] ( id bigint NOT NULL , user_id bigint NULL , created_at varchar(max) NOT NULL , updated_at varchar(max) NOT NULL , workflow_state varchar(256) NULL , account_id bigint NULL , role_id bigint NULL , meta_ts varchar(256) NOT NULL ) GO BULK INSERT [dbo].[Canvas-v2-account_users-staging] FROM 'F:\Canvas\unpackedFiles\account_users.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-account_users]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-account_users] END ; CREATE TABLE [dbo].[Canvas-v2-account_users] ( id bigint NOT NULL , user_id bigint NULL , created_at datetime NOT NULL , updated_at datetime NOT NULL , workflow_state varchar(256) NULL , account_id bigint NULL , role_id bigint NULL , meta_ts datetime NOT NULL ) GO INSERT INTO [dbo].[Canvas-v2-account_users] SELECT id , user_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 , account_id , role_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-account_users-staging] IF OBJECT_ID(N'[dbo].[Canvas-v2-account_users-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-account_users-staging] END ;