SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Copy data from one table to another table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

archana23
Yak Posting Veteran

74 Posts

Posted - 05/09/2013 :  16:02:50  Show Profile  Reply with Quote
Hi,

I have one table called Depts with column DeptId,DeptDesc,OrgId,DeptLength .. This table is having data

I am using below query to get data from Depts table.

select distinct DeptId, PARSENAME(DeptId,1),floor(DeptId) from Depts where (LEN(DeptId) - LEN( REPLACE(DeptId, '.', ''))) > 0 order by DeptId

I have anothertable called Departments its a empty table with columns DeptCode,Dept1,Dept2,DeptId. I need to fill this table with above table(Depts ) as shown below.

where DeptCode -- auto generated column with Identity increment 1
Dept1 should fill with -- floor(DeptId)
Dept2 should fill with -- PARSENAME(DeptId,1),
DeptId should fill with -- DeptId

So above Table 1 data should be copied in to Table2 as shown above.

How can i do this?

Thank you.

James K
Flowing Fount of Yak Knowledge

3559 Posts

Posted - 05/09/2013 :  16:13:33  Show Profile  Reply with Quote
You should be able to do a simple insert..select
INSERT INTO Departments (Dept1, Dept2, DeptId)
SELECT DISTINCT
        DeptId ,
        PARSENAME(DeptId, 1) ,
        FLOOR(DeptId)
FROM    Depts
WHERE   ( LEN(DeptId) - LEN(REPLACE(DeptId, '.', '')) ) > 0
ORDER BY DeptId
Go to Top of Page

archana23
Yak Posting Veteran

74 Posts

Posted - 05/09/2013 :  16:23:56  Show Profile  Reply with Quote
ok. thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 05/10/2013 :  00:29:34  Show Profile  Reply with Quote
quote:
Originally posted by James K

You should be able to do a simple insert..select
INSERT INTO Departments (Dept1, Dept2, DeptId)
SELECT DISTINCT
        DeptId ,FLOOR(DeptId),
        PARSENAME(DeptId, 1) ,
        FLOOR(DeptId)DeptId
FROM    Depts
WHERE   ( LEN(DeptId) - LEN(REPLACE(DeptId, '.', '')) ) > 0
ORDER BY DeptId




shouldnt it be modified as above to be inline with below requirement

Dept1 should fill with -- floor(DeptId)
Dept2 should fill with -- PARSENAME(DeptId,1),
DeptId should fill with -- DeptId



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

jackv
Flowing Fount of Yak Knowledge

United Kingdom
2004 Posts

Posted - 05/10/2013 :  01:36:34  Show Profile  Visit jackv's Homepage  Reply with Quote
The table that you're copying into - do you have the DDL. Watch out for any identity insrert issues. If this is a problem use: SET IDENTITY_INSERT

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000