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