USE CanvasWarehouse GO IF OBJECT_ID(N'[dbo].[Canvas-v2-pseudonyms-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-pseudonyms-staging] END ; CREATE TABLE [dbo].[Canvas-v2-pseudonyms-staging] ( id bigint NOT NULL , deleted_at varchar(max) NULL , integration_id varchar(max) NOT NULL , user_id bigint NOT NULL , created_at varchar(max) NOT NULL , updated_at varchar(max) NOT NULL , workflow_state varchar(max) NOT NULL , account_id bigint NOT NULL , sis_batch_id bigint NULL , unique_id varchar(max) NOT NULL , login_count bigint NOT NULL , failed_login_count bigint NOT NULL , last_request_at varchar(max) NULL , last_login_at varchar(max) NULL , current_login_at varchar(max) NULL , last_login_ip varchar(max) NOT NULL , current_login_ip varchar(max) NOT NULL , sis_user_id varchar(max) NOT NULL , authentication_provider_id bigint NULL , position bigint NOT NULL , meta_ts varchar(max) NOT NULL ) GO BULK INSERT [dbo].[Canvas-v2-pseudonyms-staging] FROM 'F:\Canvas\unpackedFiles\pseudonyms.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-pseudonyms]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-pseudonyms] END ; CREATE TABLE [dbo].[Canvas-v2-pseudonyms] ( id bigint NOT NULL , deleted_at datetime NULL , integration_id varchar(256) NOT NULL , user_id bigint NOT NULL , created_at datetime NOT NULL , updated_at datetime NOT NULL , workflow_state varchar(max) NOT NULL , account_id bigint NOT NULL , sis_batch_id bigint NULL , unique_id varchar(256) NOT NULL , login_count bigint NOT NULL , failed_login_count bigint NOT NULL , last_request_at datetime NULL , last_login_at datetime NULL , current_login_at datetime NULL , last_login_ip varchar(256) NOT NULL , current_login_ip varchar(256) NOT NULL , sis_user_id varchar(256) NOT NULL , authentication_provider_id bigint NULL , position bigint NOT NULL , meta_ts datetime NOT NULL ) GO INSERT INTO [dbo].[Canvas-v2-pseudonyms] SELECT id , 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 , integration_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 , sis_batch_id , unique_id , login_count , failed_login_count , CASE WHEN last_request_at IS NULL THEN NULL WHEN LEFT(last_request_at, 4) < 1900 THEN NULL ELSE CAST(last_request_at AS datetime) END as last_request_at , CASE WHEN last_login_at IS NULL THEN NULL WHEN LEFT(last_login_at, 4) < 1900 THEN NULL ELSE CAST(last_login_at AS datetime) END as last_login_at , CASE WHEN current_login_at IS NULL THEN NULL WHEN LEFT(current_login_at, 4) < 1900 THEN NULL ELSE CAST(current_login_at AS datetime) END as current_login_at , last_login_ip , current_login_ip , sis_user_id , authentication_provider_id , position , 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-pseudonyms-staging] IF OBJECT_ID(N'[dbo].[Canvas-v2-pseudonyms-staging]', N'U') IS NOT NULL BEGIN DROP TABLE [dbo].[Canvas-v2-pseudonyms-staging] END ;