简体   繁体   中英

"Arithmetic overflow error converting expression to data type nvarchar."

I'm trying to do a comparison of data between 2 tables, where I need to join multiple columns as a composite key to get a unique identifier. I'm using a CTE and the code I have is:

WITH SuburbDataTest AS (
  SELECT *
    ,  CAST(Address AS NVARCHAR(100))+' ' +CAST(LivingAddress AS NVARCHAR(2))
    + ' ' + CAST(StartDate AS NVARCHAR(11))+ ' ' +CAST(AddressTypeId AS NVARCHAR(1))
    + ' ' +CAST(SuburbId AS NVARCHAR(1))AS SuburbDataTestColumn
  FROM [mig].[ConsumerAddressMigration]
  WHERE SuburbId is NOT NULL
)
SELECT *
FROM SuburbDataTest staging
WHERE SuburbDataTestColumn IN (
  SELECT Address+' ' +CAST(LivingAddress AS NVARCHAR(2))+ ' '+CAST(StartDate AS NVARCHAR(11))
    + ' ' +CAST(AddressTypeId AS NVARCHAR(1))+ ' ' +CAST(SuburbId AS NVARCHAR(1)) AS SuburbDataTestColumn
  FROM [dbo].[tblConsumerAddress]
)

Unfortunately I'm getting

Arithmetic overflow error converting expression to data type nvarchar.

Any ideas?

This happens when you are converting a number to a string -- and the string is not big enough. I would guess this is the problem:

CAST(SuburbId AS NVARCHAR(1))

If SuburbId is a number larger than 9, then this will generate an error. Or, for that matter if the value is negative you'll get the same error as well.

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