将nvarchar转换为tinyint

时间:2017-02-22 21:33:18

标签: sql-server

我对SQL编码很陌生,我正在尝试选择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'

但是我收到以下错误:

  

转换nvarchar值时转换失败'是'数据类型tinyint。

我曾尝试投放nvarchar列,但我无法弄明白该怎么做。

你能帮帮我吗?感谢

1 个答案:

答案 0 :(得分: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'