November 25, 2011 at 3:52 am
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
November 25, 2011 at 4:04 am
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
November 25, 2011 at 4:13 am
I use webyog, couldn't find a function to help me out with this 🙁
What's wrong with that code?
November 25, 2011 at 4:28 am
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
November 25, 2011 at 4:33 am
Computed will alright
November 26, 2011 at 8:10 pm
Kind of confused here 🙁
November 26, 2011 at 8:14 pm
The "AS" phrase is producing an error :/?
November 27, 2011 at 9:01 am
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
Change is inevitable... Change for the better is not.
November 28, 2011 at 12:10 am
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
November 28, 2011 at 9:22 pm
Bump for help please
November 29, 2011 at 1:52 am
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
November 29, 2011 at 2:54 am
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