Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with sql query insertion

Posted on 2009-07-15
22
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 

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 1600 total points
ID: 24860641
No problem. Glad I can help!
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

721 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