简体   繁体   中英

How can I converting the nvarchar value to data type int

WITH FirstQuery AS (
  select i.id, o.[name]  from Item i
  LEFT OUTER JOIN sys.objects o on o.[name]='I' + cast(i.id as nvarchar(20))
  where o.name is not null
)
select PriceListItem.ProductExternalId,
       FQ.Id,   [PriceListItem].[ProductExternalDesc]
from FirstQuery FQ
     inner join [Product] on Product.ItemId = FQ.name
                         and Product.InstanceId = FQ.ID
     inner join [PriceListItem]  on Product.ID = PriceListItem.ProductId;

It has an error:

Conversion failed when converting the nvarchar value 'I451' to data type int.

So... It looks like you are trying to join on Product.ItemID (int) = FQ.name (varchar) .

Why did you think that would work?

Sample data may help people figure out what you are trying to do and how you should accomplish it.

If you still want to join on these two columns and want it to not match when it's an invalid int, maybe do it the other way around. Cast Product.ItemID to a varchar:

inner join [Product] on cast(Product.ItemID as varchar) = FQ.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM