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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 INPUT SAME INFO

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_table

In 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.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-16 : 11:23:10

I used:

Alter table xxxx
Add New_column Date

But I want to add to the new_column a date, using the same query if its possible

Thanks
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-16 : 11:56:40
If I use different queries:

A)

ALTER TABLE Existing_Table
ADD New_Column DATE


INSERT INTO Existing_Table (New_Column)
VALUES 17122012

== msg 206, level 16, state 2, line 1
Operand type clash: int is incompatible with date


B)

ALTER TABLE Existing_Table
ADD New_Column INT


INSERT INTO Existing_Table (New_Column)
VALUES 17122012

=== Just copies the 17122012 in the first row of my New_Column in my Existing_Table


I need to copy that number in all my rows from New_Column, not just in the first row

And if its possible using just one query



Go to Top of Page

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.
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-05-17 : 04:42:23
Your answer is perfect

thanks a lot!
Go to Top of Page
   

- Advertisement -