SQL conversion fail nvarchar to int

Culbertson Geoffrey 60 Reputation points
2023-04-11T14:07:56.74+00:00

Hello, I am working on the following query - SELECT DISTINCT [CM_TAN].[dbo].[v_FullCollectionMembership].[Name] AS Asset, [CM_TAN].[dbo].[v_Collections].CollectionName AS Collection_Name FROM [CM_TAN].[dbo].[v_FullCollectionMembership]LEFT JOIN [CM_TAN].[dbo].[v_Collections] ON [CM_TAN].[dbo].[v_FullCollectionMembership].CollectionID = [CM_TAN].[dbo].[v_Collections].CollectionID Order By Asset DESC I am getting this error - Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the nvarchar value 'SMS00001' to data type int. I know why but I am not sure how to fix it. It is probably simple for someone with a better understanding of SQL then I have.

SQL Server | Other
{count} votes

Accepted answer
  1. Naomi Nosonovsky 8,431 Reputation points
    2023-04-11T16:05:08.1866667+00:00

    In the LEFT JOIN condition you need to make sure that both columns are nvarchar so you need to do cast([v_FullCollectionMembership].CollectionID as nvarchar(30)) = cast([v_Collections].CollectionID as nvarchar(30)) The query will be slower with that, but should work.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.