locked
How to copy from one column to another - query? RRS feed

  • Question

  • Hi, I am writing to seek help, in creating a query or function which allows me to match certain columns from 2 different table (reference table and market table) and if the match is successful, then transfer a specific data from one table to another.

    For example, I would like to transfer column ‘shortid’ from reference table into the Market table, if the column shortname matches between the two tables.

    Expected output:

    So far, I have tried doing this using join clause for the both short names but I can seem to figure out how to do copy one column to another based on condition. 

    Any advice would be very much appreciated. Thank you

    Tuesday, October 18, 2016 12:52 PM

Answers

  • Try this:

    Insert into marketable(Data1, data2, ShortName, ShortID)

    select MarketTable.Data1, MarketTable.Data2, ReferenceTable.Shortname, ReferenceTable.ShortID from ReferenceTable join

    MarketTable on MarketTable.ShortName=ReferenceTable.ShortName

    Tuesday, October 18, 2016 12:59 PM
  • Try this. You can also use the MERGE statement

    update mt set mt.shortid = rt.shortid 
    		from MarketTable mt inner join ReferenceTable rt on mt.ShortName=rt.ShortName

    Tuesday, October 18, 2016 1:08 PM
  •  Hi Missy786,

    you can use update comand to data in balck collumn based on condition.
    
    


    Refer example.


    Please click Mark As Answer if my post helped.


    Tuesday, October 18, 2016 1:14 PM

All replies

  • Try this:

    Insert into marketable(Data1, data2, ShortName, ShortID)

    select MarketTable.Data1, MarketTable.Data2, ReferenceTable.Shortname, ReferenceTable.ShortID from ReferenceTable join

    MarketTable on MarketTable.ShortName=ReferenceTable.ShortName

    Tuesday, October 18, 2016 12:59 PM
  • Try this. You can also use the MERGE statement

    update mt set mt.shortid = rt.shortid 
    		from MarketTable mt inner join ReferenceTable rt on mt.ShortName=rt.ShortName

    Tuesday, October 18, 2016 1:08 PM
  •  Hi Missy786,

    you can use update comand to data in balck collumn based on condition.
    
    


    Refer example.


    Please click Mark As Answer if my post helped.


    Tuesday, October 18, 2016 1:14 PM