'Conversion failed when converting from a character string to uniqueidentifier Left Join
I very rarely do SQL and am trying to do a left join on two tables in SSMS
Two databases and one has column ACT_HistoryID__c as type varchar(255) and the other database has a column HistoryID as PK uniqueidentifier
When I do:
SELECT H.[ActivityDate]
,H.ACT_HistoryID__c
,D.HISTORYID
,D.DURATION
FROM [Sales_Import].[dbo].[Task_History] as H
left join [National_MDB_26].[dbo].[HISTORY] as D
on H.ACT_HistoryID__c = D.HISTORYID
I get Msg 8169, Level 16, State 2, Line 2 Conversion failed when converting from a character string to uniqueidentifier
I have tried casting converting with no success although my limited understanding is that SQL should attempt the conversion itself.
I have ran the below SQL to check if there is a corrupt value and it returns all rows:
SELECT [ActivityDate]
FROM [Sales_Import].[dbo].[Task_History]
WHERE TRY_CONVERT(UNIQUEIDENTIFIER, ACT_HistoryID__c) IS NOT NULL;
I have copied the ACT_HistoryID_c value and performed a lookup which returned a row
SELECT [HISTORYID]
FROM [National_MDB_26].[dbo].[TBL_HISTORY]
where HISTORYID = '5B437BBC-3D78-49F8-8365-59480A57410A'
I presume it's something stupid however any insights in where I.m failing?
CREATE TABLE [Task_History_V2] (
[ActivityDate] varchar(255),
[ACT_Attachment_Display__c] varchar(255),
[ACT_Attachment_Filename__c] varchar(255),
[ACT_Attachment_URL__c] varchar(255),
[ACT_CreateDate] varchar(255),
[ACT_EditDate] varchar(255),
[ACT_GroupID__c] varchar(255),
[ACT_HistoryID__c] varchar(255),
[ACT_RecordMgr__c] varchar(255),
[ACT_Type__c] varchar(255),
[ACT_WhatID_Op__c] varchar(255),
[ACT_WhatID__c] varchar(255),
[ACT_WhoID__c] varchar(255),
[CreatedByID] varchar(255),
[CreatedDate] varchar(255),
[Description] varchar(255),
[LastModifiedByID] varchar(255),
[LastModifiedDate] varchar(255),
[Outlookid__c] varchar(255),
[OwnerID] varchar(255),
[Status] varchar(255),
[Subject] varchar(255),
[TasksubType] varchar(255),
[X_Companies_Involved] varchar(255),
[X_Company] varchar(255),
[X_Contact] varchar(255),
[X_Contacts_Involved] varchar(255),
[X_Has_Multiples] varchar(255),
[X_IsPrivate] varchar(255)
)
CREATE TABLE [dbo].[TBL_HISTORY](
[HISTORYID] [uniqueidentifier] NOT NULL,
[HISTORYTYPEID] [smallint] NOT NULL,
[ISPRIVATE] [tinyint] NOT NULL,
[STARTTIME] [smalldatetime] NOT NULL,
[ENDTIME] [smalldatetime] NOT NULL,
[REGARDING] [nvarchar](256) NULL,
[DETAILS] [nvarchar](max) NULL,
[ACCESSOR_ACTIVITY_CLEAREDID] [uniqueidentifier] NULL,
[MANAGEUSERID] [uniqueidentifier] NOT NULL,
[CREATEUSERID] [uniqueidentifier] NOT NULL,
[CREATEDATE] [datetime] NOT NULL,
[EDITUSERID] [uniqueidentifier] NULL,
[EDITDATE] [datetime] NULL,
[DURATION] AS (datediff(minute,[STARTTIME],[ENDTIME])),
[OUTLOOKID] [uniqueidentifier] NULL,
CONSTRAINT [TBL_HISTORY_PK] PRIMARY KEY NONCLUSTERED
(
[HISTORYID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Solution Not Answer I inserted the table to my Sales_Import database and converted the Guid to a string and my join then worked...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|