Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-05-16 : 10:54:32
|
| Hi there! I need to create a new_column in my People_tableIn the new_column that im creating, I need to input a date using a query, no matter how many number of rows do I have for that table, I need each of my cells, with a date.Any ideas?Thanks |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-05-16 : 11:04:40
|
| add the column.Then, update the table with your query.How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-05-16 : 11:23:10
|
| I used: Alter table xxxxAdd New_column DateBut I want to add to the new_column a date, using the same query if its possibleThanks |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-05-16 : 11:56:40
|
| If I use different queries: A) ALTER TABLE Existing_TableADD New_Column DATEINSERT INTO Existing_Table (New_Column)VALUES 17122012== msg 206, level 16, state 2, line 1Operand type clash: int is incompatible with date B)ALTER TABLE Existing_TableADD New_Column INTINSERT INTO Existing_Table (New_Column)VALUES 17122012=== Just copies the 17122012 in the first row of my New_Column in my Existing_TableI need to copy that number in all my rows from New_Column, not just in the first rowAnd if its possible using just one query |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-16 : 12:41:02
|
Two things you need to change:First, when you want to UPDATE a column for the already existing rows in your table, you should use the UPDATE statement rather than INSERT statement.Second, 17122012 is interpreted by SQL Server as being an integer, and it does not do implicit conversion from integer to DATE. Even if you did, you wouldn't want that. SQL Server can implicitly convert character data to DATE. So your query should be something like this:UPDATE Existing_Table SET New_Column = '20121217'; This will update the New_Column to be December 17, 2012 in EVERY existing row in your table.Also, specify the date in the YYYYMMDD format - which is unambiguous format recommended by experts as the preferred choice. |
 |
|
|
jfm
Posting Yak Master
145 Posts |
Posted - 2012-05-17 : 04:42:23
|
| Your answer is perfectthanks a lot! |
 |
|
|
|
|
|
|
|