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
 help in inserting data

Author  Topic 

shravanigonela
Starting Member

2 Posts

Posted - 2009-03-09 : 03:23:30
hi, i am new to sql server, .............

i have 2 databases,i have to insert data from the old database to new database........
for example i have first_name column in new database but i have first_name,first_name1,first_name2 ... 3 columns in old database...... i have to insert 3 columns into the 1 column in new database.......

i have done like this .......
1. In the dataflow
task i have taken oledb source in which i have selected 3 columns and sorted it...
2.on the side taken another oledb1 source which have 1 column first_name and sorted.....

what should i do to insert 3 colums in to the one column.........
please help me urgently...........

guptam
Posting Yak Master

161 Posts

Posted - 2009-03-09 : 03:36:46
Are both databases on same server?

If so you can do ...

INSERT INTO NewDBName.dbo.Table (First_Name) SELECT first_name FROM OldDB.dbo.Table
INSERT INTO NewDBName.dbo.Table (First_Name) SELECT first_name1 FROM OldDB.dbo.Table
INSERT INTO NewDBName.dbo.Table (First_Name) SELECT first_name2 FROM OldDB.dbo.Table


--
Mohit K. Gupta
B.Sc. CS, Minor Japanese
MCITP: Database Administrator
MCTS: SQL Server 2005
http://sqllearnings.blogspot.com/
Go to Top of Page

matty
Posting Yak Master

161 Posts

Posted - 2009-03-09 : 04:27:42
INSERT INTO NewDB.dbo.Table (First_Name) SELECT first_name + ' ' + first_name1 + ' ' + first_name2 FROM OldDB.dbo.Table
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-09 : 07:15:29
just modification to matty query
try like this
SELECT ISNULL(first_name ,'') + ' ' + ISNULL(first_name1,'')+ ' ' + ISNULL(first_name2,'') FROM OldDB.dbo.Table
SELECT COALESCE(first_name ,'') + ' ' + COALESCE(first_name1,'')+ ' ' + COALESCE(first_name2,'') FROM OldDB.dbo.Table
to avoids nulls when concatenating the varchar fields
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-10 : 02:39:38
or did you meant this?

INSERT INTO NewDB.dbo.Table (First_Name)
SELECT first_name FROM OldDB.dbo.Table
UNION
SELECT first_name1 FROM OldDB.dbo.Table
UNION
SELECT first_name2 FROM OldDB.dbo.Table
Go to Top of Page
   

- Advertisement -