Create a new column that concatenates a phrase with an integer

  • Hello,

    I'd like to make a new column, let's call it TestColumn, in my table called TestTable, and I want the default values within this new column to concatenate a sample phrase, like 'You owe $', and an integer from another column in TestTable, call it Debt

    so, I know that the code for concatenation is like this:

    SELECT ('You owe $' + Debt) AS TestColumn

    FROM TestTable

    How do I make a new column called TestColumn with this default value? The above code produces floating results

    Thank you very much

  • CREATE TABLE #TestTable (

    Debt money

    )

    INSERT INTO #TestTable (Debt) VALUES (1)

    INSERT INTO #TestTable (Debt) VALUES (2)

    INSERT INTO #TestTable (Debt) VALUES (3)

    ALTER TABLE #TestTable ADD TestColumn AS 'You owe $ ' + CAST(Debt AS varchar(50))

    SELECT *

    FROM #TestTable

    I don't recommend doing this, though.

    You'd better do this kind of things on the application side.

    Hope this helps

    Gianluca

    -- Gianluca Sartori

  • I use webyog, couldn't find a function to help me out with this 🙁

    What's wrong with that code?

  • You can't have a default expression containing the name of a column, so if a computed column isn't what you're after, I think you can only achieve what you want with a trigger.

    John

  • Computed will alright

  • Kind of confused here 🙁

  • The "AS" phrase is producing an error :/?

  • mahdib21 (11/26/2011)


    The "AS" phrase is producing an error :/?

    First, post the code that you're actually having a problem with. Lots of folks make a mistake when copying code.

    Second, are you using SQL Server or some other database engine?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Using code similar to that posted above:

    ALTER TABLE TestTable ADD TestColumn AS'You owe $'+CONVERT(VARCHAR(9),Debt)

    SELECT *

    FROM TestTable

    I've already created the table

    Sorry, don't know much about SQL

    I believe I'm using and SQL server, I'm guessing at least. What other database could I be using? It's MySQL, I access it with SQLYog

    Thank you very much for your help btw 🙂 all of you

  • Bump for help please

  • You really need to find out what RDBMS you're using. It sounds like it's MySQL, in which case you may not find anyone here who can help - this is a SQL Server forum.

    John

  • I do think it's MySQL. Sorry 🙁 Didn't know, thank you

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

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