Win has asked for the wisdom of the Perl Monks concerning the following question:

I wish to take all the values from one table column and place them all into another table column (using MS SQL Server). However, I also wish to CONCAT the value 'A' to the start of each of the cell contents that is input into the second table. Is there an SQL solution to this or do I have to write a Perl script via DBI to do this. I am all set up for the Perl option.

Replies are listed 'Best First'.
Re: CONCAT Perl or SQL
by mpeppler (Vicar) on Dec 03, 2003 at 15:19 UTC
    Is the destination column in the same table, or a different table?

    If it's the same table, you can do something like this:

    update table_name set destination_column = "A" + source_column where some_condition
    You can also do the same thing if the source and destination columns are in different tables. I suggest going to Microsoft's web site and reading up on the UPDATE command in T-SQL.

    Michael

      I have tried several variations on a theme including:

      my $sthG = $dbh->prepare("INSERT into ICD_9_Disease_Types (ICD_9_code) + SELECT CONCAT('A', select ICD_9 from ICD_9 where ICD_9 > 0);") or di +e "Couldn't prepare query: ".$dbh->errstr;

      I am transferring between two different tables.

        Please go read the Transact-SQL manual.

        I just showed you above that you concatenate strings in SQL using the '+' operator:  select "A" + ICD_9 ...

        You are at a point where you don't yet understand SQL - I strongly suggest that until you understand SQL you leave perl and DBI out of it, and write the queries that you want to execute in a text editor and run them using isql.exe or some other tool that connects directly to the database. This will give you direct feedback on what you are doing.

        Are you inserting a NEW row in the ICD_9_Disease_Types table, or are you updating an existing row?

        From the code above I am guessing that you are updating an existing row, in which case you need to know the relationship between the two tables.

        Michael

        A reply falls below the community's threshold of quality. You may see it by logging in.
        my $sql = " UPDATE ICD_9_Disease_Types SET ICD_9_code = CONCAT('A', ICD_9_code) WHERE ICD_9 > 0"; my $sth = $dbh->prepare($sql) or $dbh->errstr(); my $rows_updated = $sth->execute(); print "We updated $rows_updated rows\n";