"Conversion failed when converting the varchar value to data type int

  • tmc

    SSC Enthusiast

    Points: 172

    Hello to everyone

    I am started my new carrer as data analyst using SQL.

    am new entry and i have to join the following charts


    i used this code




    from Negozi

    inner join StrutturaOrganizzativa

    on StrutturaOrganizzativa.descrizione=Negozi.fk_responsabile

    But unfortunately SQL give me back this result

    Msg 245, Level 16, State 1, Line 1

    Conversion failed when converting the varchar value 'Mario Rossi' to data type int.

    Please  anyone can tell me where i wrong trying to solve it ?

    Thank a lot !



  • Phil Parkin

    SSC Guru

    Points: 244578

    Sure. This join

    on StrutturaOrganizzativa.descrizione=Negozi.fk_responsabile

    is the problem, because you're trying to match a string and an integer. Columns which you JOIN on should ideally have the same datatype.

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.
    See https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help/ for details of how to post T-SQL code-related questions.

  • Sumathi

    SSC Rookie

    Points: 28

    StrutturaOrganizzativa.descrizione is string type and Negozi.fk_responsabile is integer type. When you join on two columns both should be of same datatype. Use this:




    from Negozi

    inner join StrutturaOrganizzativa

    on StrutturaOrganizzativa.pk_id = Negozi.pk_id

  • Eddie Wuerch


    Points: 12380

    I'm guessing the value in Negozi.fk_responsabile points to the primary key in StrutturaOrganizzativa (StrutturaOrganizzativa.pk_id)

    'pk' in relational databases often stands for 'Primary Key', and 'fk' often means 'Foreign Key'.

    A Primary Key is a unique value that identifies a row in a table. The 'pk_id' column is probably the Primary Key in the StrutturaOrganizzativa table, and can be used to uniquely identify any row in the table. The 'fk_responsabile' column in StrutturaOrganizzativa is likely a Foreign Key, meaning 'use this value to find the row in another place'. So we'll JOIN the two tables by connecting the foreign key to the remote primary key.

    Note the change to the JOIN clause:

    Select Negozi.descrizione, StrutturaOrganizzativa.descrizione
    from Negoziinner
    join StrutturaOrganizzativa on StrutturaOrganizzativa.pk_id=Negozi.fk_responsabile


    Eddie Wuerch
    MCM: SQL

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply