2017-02-20 48 views
0

當我試圖插入另一個表中的信息時,它應該是空的時候從date列返回'1900-01-01'。在導入表上,數據類型爲varchar,我想在表中插入信息是date數據類型。有在EndDate列一些空值應插入爲空,但它返回「1900-01-01」SQL Server:插入日期返回錯誤的值

Insert into tblServiceHistory (PersonID, PositionID, CommitteeID,StartDate, EndDate) 
    Select distinct 
     PersonID, PositionID, CommitteeID, StartDate, EndDate 
    From 
     tblCommitteeOfficers_IMPORT 
    Where 
     PersonID is not null 
     And StartDate is not null 
     And PositionID is not null 
     And CommitteeID is not null 
+0

你會出現在你的數據的價值'1900-01-01'。 –

回答

2

轉換爲日期空白弦翻中'1900-01-01'

例如select convert(date,'')返回'1900-01-01'

您可以使用nullif()在您的select中將空字符串變成null值。

試試這個:

Insert into tblServiceHistory (PersonID, PositionID, CommitteeID,StartDate, EndDate) 
Select distinct PersonID, PositionID, CommitteeID, nullif(StartDate,''), nullif(EndDate,'') 
From tblCommitteeOfficers_IMPORT 
Where PersonID is not null 
    AND nullif(StartDate,'') is not null 
    AND PositionID is not null 
    AND CommitteeID is not null 
+0

It works thanks –

+0

@TimothyWong樂意幫忙! – SqlZim