2017-02-22 286 views
0

我對SQL編碼有點新,我試圖選擇一個nvarchar值並將其插入到tinyint列中。將nvarchar轉換爲tinyint

我用下面的查詢

insert into COV (GID, DocumentNumber, RegistrationDate, CustomerCode, CustomerName, DeliveryDateAndTime, LineGID, Item, Quantity, DeliveryDate, MU, ExportedToSchnell) 
    select 
     esd.GID, esd.ADCode, esd.ADRegistrationDate, esc.Code, esc.Name, 
     esd.DeliveryDueDate, esfl.GID, esf.Code, esfl.Quantity, 
     esfl.DeliveryDate, esmm.fMUCode, esfl.Stringfield1 
    from 
     ESFIDocumentTrade esd 
    left join 
     ESFITradeAccount esc on esd.fTradeAccountGID = esc.GID 
    left join 
     ESFIDocumentType est on esd.fADDocumentTypeGID = est.GID 
    left join 
     ESFILineItem esfl on esfl.fDocumentGID = esd.GID 
    left join 
     ESFIItem esf on esf.GID = esfl.fItemGID 
    left join 
     ESMMItemMU esmm on esmm.fItemGID = esf.GID 
    where 
     est.Code = 'COV' 
     and esfl.StringField1 = 'YES' 
     and esd.ADRegistrationDate > '2017-02-01' 

,但我得到了以下錯誤:

Conversion failed when converting the nvarchar value 'YES' to data type tinyint.

我試圖施放nvarchar列,但我無法弄清楚如何做到這一點。

你能幫我嗎?由於

回答

3

首先,我認爲SqlZim回答是好。

但我只是想在這裏添加一些東西。

首先,在連接中,當您在where子句中使用左連接右側的表的值時,它是內連接,而不是左連接。 其次,在這種情況下,由於where子句,不需要CASE語句,只需在選擇列表中放置1代替esfl.Stringfield1即可。

insert into COV (GID, DocumentNumber, RegistrationDate, CustomerCode, CustomerName, DeliveryDateAndTime, LineGID, Item, Quantity, DeliveryDate, MU, ExportedToSchnell) 
select esd.GID, esd.ADCode, esd.ADRegistrationDate, esc.Code, esc.Name, 
esd.DeliveryDueDate, esfl.GID, esf.Code, esfl.Quantity, esfl.DeliveryDate, 
esmm.fMUCode, 1 
from ESFIDocumentTrade esd 
inner join ESFIDocumentType est on esd.fADDocumentTypeGID=est.GID 
inner join ESFILineItem esfl on esfl.fDocumentGID=esd.GID 
left join ESFITradeAccount esc on esd.fTradeAccountGID=esc.GID 
left join ESFIItem esf on esf.GID=esfl.fItemGID 
left join ESMMItemMU esmm on esmm.fItemGID=esf.GID 
where est.Code='COV' and esfl.StringField1='YES' and esd.ADRegistrationDate>'2017-02-01' 
+1

SzlZim?那是誰 ? :P – SqlZim

+1

@SqlZim我的地形。我修好了它。 – DVT

+0

好點,+1 – SqlZim

1

如果需要的值更改爲1,其中Stringfield1 = 'Yes',那麼你可以用case表達做到這一點很容易:

insert into COV (
    GID 
, DocumentNumber 
, RegistrationDate 
, CustomerCode 
, CustomerName 
, DeliveryDateAndTime 
, LineGID 
, Item 
, Quantity 
, DeliveryDate 
, MU 
, ExportedToSchnell 
) 
select 
    esd.GID 
, esd.ADCode 
, esd.ADRegistrationDate 
, esc.Code 
, esc.name 
, esd.DeliveryDueDate 
, esfl.GID 
, esf.Code 
, esfl.Quantity 
, esfl.DeliveryDate 
, esmm.fMUCode 
, case when esfl.Stringfield1 = 'YES' then 1 else 0 end 
from ESFIDocumentTrade esd 
    left join ESFITradeAccount esc 
    on esd.fTradeAccountGID = esc.GID 
    left join ESFIDocumentType est 
    on esd.fADDocumentTypeGID = est.GID 
    left join ESFILineItem esfl 
    on esfl.fDocumentGID = esd.GID 
    left join ESFIItem esf 
    on esf.GID = esfl.fItemGID 
    left join ESMMItemMU esmm 
    on esmm.fItemGID = esf.GID 
where est.Code = 'COV' 
    and esfl.StringField1 = 'YES' 
    and esd.ADRegistrationDate > '2017-02-01' 
+0

哇。它工作順利。謝謝 – cdrrrrr

+0

@cdrrrr樂於幫忙! – SqlZim