Help in using Update/Set and variables

  • I have the following code:

    /* CustPmt - Copy the Payment Number into the Comment Fiefd */

    declare @oldcomment varchar(50);

    declare @newcomment varchar(50);

    declare @trancmt varchar(50);

    declare @custpmtno varchar(50);

    begin

    update stgCustpmt

    set @oldcomment = TranCmt;

    set@custpmtno = cast(CustPmtNo as varchar);

    set@newcomment = 'OldPmtNo: '+cast(CustPmtNo as varchar)+' Cmt: '+@oldcomment;

    setTranCmt = @newcomment;

    end

    I want to go thru each row, and copy the pmntno field into a format and then put it into the trancmt field.

    The problem that I am running into is it doesn't like the set that actuatlly updates the column.

    Is there another way I can/should be doing this?

    Thanks,

    Phillip

  • This should do what you want:

    [font="Courier New"]UPDATE stgCustpmt

       SET TranCmt = 'OldPmtNo: ' + CAST(CustPmtNo AS VARCHAR(20)) + ' Cmt: ' + TranCmt[/font]

  • Jack,

    You are quickly becoming my hero. 😀

    However, when I try it that way, all the comment are blank. I can do just the TranCmnt = CustPmtNo and that works, but building it up doesn't. 🙁

  • When you say blank I am going to assume you mean NULL as Concatenating to a NULL yields NULL. So if there is a possibility that one or both of your columns in the concatenation could contain NULLS you need to use the ISNULL() function (IsNull(TranCmt, '')).

  • That helped, I had to throw another cast in there to truncate the entire value, but isnull did the trick.

    Thanks,

    Phillip

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

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