The following query works perfectly,
insert into [EGallery].dbo.[CustomerDetails]
Select Distinct B.CountyB as 'Mobile' , Cast(BuildingB as Varchar(100)) as 'Email' ,
A.CardCode , A.CardName as 'First Name' , '' as 'Last Name' ,
'' as Gender , Cast(A.Address as Varchar(1000)) as 'Address' , Convert(Varchar(10), A.U_BirthDay,105) as 'birthday' ,
Convert(Varchar(10), A.U_AnnivDay ,105) as 'Anniversary' ,
Case
When A.CardCode Like '%%'+ C.WhsCode +'%%' Then Convert(Varchar(10) , A.DocDate ,105)
Else Convert(Varchar(10), (Select X.CreateDate From OCRD X Where X.CardCode = A.CardCode) ,105) End as 'JoinDate' ,
C.WhsCode as 'JoinStore','Open' as Status ,(Select GETDATE()) as CreatedDateTime,(Select GETDATE()) as ProcessDateTime, '' as StatusMSg
From OINV A
Inner Join INV12 B On A.DocEntry = B.DocEntry
Inner Join INV1 C On A.DocEntry = C.DocEntry
Where C.LineNum = '0'
--B.CountyB not in(select D.Mobile from [EGallery].dbo.[CustomerDetails] D where D.Mobile=B.CountyB)
--not exists (select Mobile from [EGallery].dbo.[CustomerDetails] D where D.Mobile=B.CountyB)
But before I insert records into the [EGallery].dbo.[CustomerDetails] table, I need to check whether the phone number already exists in the table. If the record already exists, there is no need to insert it again. For that I have added one more condition (which I have commented out in the query) but it reports this error while running the query:
Cannot resolve the collation conflict between "SQL_Latin1_General_CP850_CI_AS" and "Latin1_General_CI_AI" in the equal to operation.