Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Help with sql query insertion

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
parpaa
Asked:
parpaa
  • 11
  • 9
  • 2
1 Solution
 
pssandhuCommented:
Can you please give us an example of how the data in PRIMARY_PHONE looks like?
P.
0
 
parpaaAuthor Commented:
4326734565, now it has to split into 432, 673, 4565.
Thanks
0
 
chapmandewCommented:

select leftpart = left(fieldname,3), midpart = substring(fieldname, 4, 3), rightpart = right(fieldname, 4)
from tablename
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
parpaaAuthor Commented:
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
 
pssandhuCommented:
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
 
parpaaAuthor Commented:
cant i mention this during insertion, if i get the whole phone number from the source(i.e oracle)
0
 
chapmandewCommented:
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
 
parpaaAuthor Commented:
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
 
pssandhuCommented:
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
 
pssandhuCommented:
Oh, okay... so the insert statment is in Oracle not in SQL Server.... then some syntax has to change...
P.
0
 
parpaaAuthor Commented:
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
 
parpaaAuthor Commented:
it says right is an invalid identifier.. thanks for taking a look into this man!! appreciate your time..
0
 
pssandhuCommented:
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
 
parpaaAuthor Commented:
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
 
parpaaAuthor Commented:
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
 
pssandhuCommented:
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
 
parpaaAuthor Commented:
but i tried to run the above code locally on oracle instance and it return an invalid right identifier error..
0
 
pssandhuCommented:
Yes, because I am posting in SQL Server syntax. Let me see if I can post this in correct Oracle syntax.
P.
0
 
pssandhuCommented:
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
 
parpaaAuthor Commented:
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
 
parpaaAuthor Commented:
Thanks man, it worked.. appreciate your help.. i'll dig this logic and try to implement into rest..
0
 
pssandhuCommented:
No problem. Glad I can help!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

  • 11
  • 9
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now