Solved

Help with sql query insertion

Posted on 2009-07-15
22
193 Views
Last Modified: 2012-05-07
I have a situation here, on a daily basis i need to import data from oracle 9i tables into our sql server 2005 table, but the primary phone from source has to be split into phone area, phone, phone extn in destination and also the primary name into firstname and last name by mentioning it during insertion, can you help me with that please? hope i am clear..


INSERT INTO SQL.TABLE
           (FIRST_NAME,LAST_NAME,STATUS,EMAIL_ADDRESS,ADDRESS1,ADDRESS2,CITY,STATE,COUNTRY,
ZIPCODE,ENTERED_BY,PHONE_AREA,PHONE,PHONE_EXT)
   

SELECT PRIMARY_NAME, PRIMARY_EMAIL,PRIMARY_ADDRESS1,PRIMARY_ADDRESS2,PRIMARY_CITY,PRIMARY_STATE,PRIMARY_ZIP,
PRIMARY_PHONE,PRIMARY_FAX FROM CUSTOMERS
FROM ORACLE.TABLE
0
Comment
Question by:parpaa
  • 11
  • 9
  • 2
22 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24859864
Can you please give us an example of how the data in PRIMARY_PHONE looks like?
P.
0
 

Author Comment

by:parpaa
ID: 24859924
4326734565, now it has to split into 432, 673, 4565.
Thanks
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24859947

select leftpart = left(fieldname,3), midpart = substring(fieldname, 4, 3), rightpart = right(fieldname, 4)
from tablename
0
 

Author Comment

by:parpaa
ID: 24859974
actually it could be like (432)6734565 or 432-673-4565 or 43267-34565 , so in any case i have to split into 3,3,4
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24859975
Please run this select statment to see if this is what you are looking for:
SELECT	SUBSTRING(PRIMARY_NAME, 1, LEN(PRIMARY_NAME)-CHARINDEX(' ', REVERSE(PRIMARY_NAME))) as FirstName, 

		SUBSTRING(PRIMARY_NAME, (LEN(PRIMARY_NAME)+2)-CHARINDEX(' ', REVERSE(PRIMARY_NAME)), LEN(PRIMARY_NAME)) as LastName,

		PRIMARY_EMAIL,

		PRIMARY_ADDRESS1,

		PRIMARY_ADDRESS2,

		PRIMARY_CITY,

		PRIMARY_STATE,

		PRIMARY_ZIP,

		LEFT(CAST(PRIMARY_PHONE as Varchar(20)),3) as PhoneArea,

		SUBSTRING(CAST(PRIMARY_PHONE as Varchar(20)),4,3) as Phone,

		RIGHT(CAST(PRIMARY_PHONE as Varchar(20)),4) as PhoneExtn,         

		PRIMARY_FAX 

FROM CUSTOMERS

Open in new window

0
 

Author Comment

by:parpaa
ID: 24859992
cant i mention this during insertion, if i get the whole phone number from the source(i.e oracle)
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24860006
select leftpart = left(newphone ,3), midpart = substring(newphone , 4, 3), rightpart = right(newphone , 4)
from
(
select *, newphone = replace(replace(replace(phone, '(', ''), ')',''), '-', '')
from tablename
) a
0
 

Author Comment

by:parpaa
ID: 24860028
if i am doing the select then it has to be in pl-sql, thatsy i wanted to implement thisduring insertion which is into sql table.. this is not working for pl-sql..
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24860041
Lets see:

SELECT	SUBSTRING(PRIMARY_NAME, 1, LEN(PRIMARY_NAME)-CHARINDEX(' ', REVERSE(PRIMARY_NAME))) as FirstName, 

		SUBSTRING(PRIMARY_NAME, (LEN(PRIMARY_NAME)+2)-CHARINDEX(' ', REVERSE(PRIMARY_NAME)), LEN(PRIMARY_NAME)) as LastName,

		PRIMARY_EMAIL,

		PRIMARY_ADDRESS1,

		PRIMARY_ADDRESS2,

		PRIMARY_CITY,

		PRIMARY_STATE,

		PRIMARY_ZIP,

		LEFT(REPLACE(Replace(Replace(Replace(CAST(ISNULL(PRIMARY_PHONE,'0000000000') as Varchar(20)),'-',''),'(',''),')',''),' ',''),3) as PhoneArea,

		SUBSTRING(REPLACE(Replace(Replace(Replace(CAST(ISNULL(PRIMARY_PHONE,'0000000000') as Varchar(20)),'-',''),'(',''),')',''),' ',''),4,3) as PhoneArea,

		RIGHT(REPLACE(Replace(Replace(Replace(CAST(ISNULL(PRIMARY_PHONE,'0000000000') as Varchar(20)),'-',''),'(',''),')',''),' ',''),4) as PhoneArea         

		PRIMARY_FAX 

FROM CUSTOMERS

Open in new window

0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24860052
Oh, okay... so the insert statment is in Oracle not in SQL Server.... then some syntax has to change...
P.
0
 

Author Comment

by:parpaa
ID: 24860064
NO, you are confused, select is from oracle and insert is into sql server, and i have to do this for around 20 tables, for most of them its a direct import except a few in which i have toi split the data during insertion.. am i clear?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:parpaa
ID: 24860100
it says right is an invalid identifier.. thanks for taking a look into this man!! appreciate your time..
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24860142
No Problem!
So you are running an INSERT statement in the SQL Server. That to me means that you have a linked server set up with Oracle datasource. In that case (based on your setup) you should be able to run select statements in SQL Server on oracle tables. Correct?
Also, please try the query again. I was missing a comma:

SELECT	SUBSTRING(PRIMARY_NAME, 1, LEN(PRIMARY_NAME)-CHARINDEX(' ', REVERSE(PRIMARY_NAME))) as FirstName, 

		SUBSTRING(PRIMARY_NAME, (LEN(PRIMARY_NAME)+2)-CHARINDEX(' ', REVERSE(PRIMARY_NAME)), LEN(PRIMARY_NAME)) as LastName,

		PRIMARY_EMAIL,

		PRIMARY_ADDRESS1,

		PRIMARY_ADDRESS2,

		PRIMARY_CITY,

		PRIMARY_STATE,

		PRIMARY_ZIP,

		LEFT(REPLACE(Replace(Replace(Replace(CAST(ISNULL(PRIMARY_PHONE,'0000000000') as Varchar(20)),'-',''),'(',''),')',''),' ',''),3) as PhoneArea,

		SUBSTRING(REPLACE(Replace(Replace(Replace(CAST(ISNULL(PRIMARY_PHONE,'0000000000') as Varchar(20)),'-',''),'(',''),')',''),' ',''),4,3) as Phone,

		RIGHT(REPLACE(Replace(Replace(Replace(CAST(ISNULL(PRIMARY_PHONE,'0000000000') as Varchar(20)),'-',''),'(',''),')',''),' ',''),4) as PhoneExtn,         

		PRIMARY_FAX 

FROM CUSTOMERS

Open in new window

0
 

Author Comment

by:parpaa
ID: 24860200
as of now i didnt have the linked server setup, i am selecting select locally on oracle instance and for data import i have created a ssis package so that it does the selection in one step and insertion in one step, i am not sure if this is the correct way..
0
 

Author Comment

by:parpaa
ID: 24860239
so in this case you mean i can do a t-sql select from oracle instance through linked server? i was of the assumption that we have to code it differently i./e tsql and pl-sql and link them..
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24860376
Yes. So I would actually prefer creating a linked server in SQL Server if this is a daily overnight job to import data and you are using SQL Server for all your reporting and data analysis. Once the linked server is setup, you can just write a simple stored proc which will transform and insert data on your local server/database. You can then schedule the run of the stored procedure in SQL Server Agent/Jobs.
This will also give you the functionality of queryng the Oracle database from sql server and you won't have to switch back and forth.
P.
0
 

Author Comment

by:parpaa
ID: 24860407
but i tried to run the above code locally on oracle instance and it return an invalid right identifier error..
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24860456
Yes, because I am posting in SQL Server syntax. Let me see if I can post this in correct Oracle syntax.
P.
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24860570
Alright, so I do not have an oracle instance running on my pc so will have trouble shoot any syntax erros. But hopefully this should do it:



SELECT	SUBSTR(PRIMARY_NAME, 1, LENGTH(PRIMARY_NAME)-INSTR(REVERSE(PRIMARY_NAME),' ')) as FirstName, 

		SUBSTR(PRIMARY_NAME, (LENGTH(PRIMARY_NAME)+2)-INSTR(REVERSE(PRIMARY_NAME), ' '), LENGTH(PRIMARY_NAME)) as LastName,

		PRIMARY_EMAIL,

		PRIMARY_ADDRESS1,

		PRIMARY_ADDRESS2,

		PRIMARY_CITY,

		PRIMARY_STATE,

		PRIMARY_ZIP,

		SUBSTR(REPLACE(Replace(Replace(Replace(CAST(PRIMARY_PHONE as Varchar2(20)),'-'),'('),')'),' '),1,3) as PhoneArea,

		SUBSTR(REPLACE(Replace(Replace(Replace(CAST(PRIMARY_PHONE as Varchar2(20)),'-'),'('),')'),' '),4,3) as Phone,

		SUBSTR(REPLACE(Replace(Replace(Replace(CAST(PRIMARY_PHONE as Varchar2(20)),'-'),'('),')'),' '),-4) as PhoneExtn,         

		PRIMARY_FAX 

FROM CUSTOMERS

Open in new window

0
 

Author Comment

by:parpaa
ID: 24860573
we've got some firewall issues with the oracle instance, a ticket is in progress, so once that gets resolved i'll create linked server and test it by writing a stored proc..
0
 

Author Comment

by:parpaa
ID: 24860589
Thanks man, it worked.. appreciate your help.. i'll dig this logic and try to implement into rest..
0
 
LVL 17

Accepted Solution

by:
pssandhu earned 400 total points
ID: 24860641
No problem. Glad I can help!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now