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

x
?
Solved

SQL Query mechanasism needs attention

Posted on 2016-09-05
5
Medium Priority
?
62 Views
Last Modified: 2016-09-14
I have a SQL Query linked server job which I run on the hour, below is a simple process that updates some user info into a separate table that a 3rd party vendor uses for cross reference.

delete from [ZapCentral].dbo.SDContractParkers
insert into [ZapCentral].dbo.SDContractParkers
Select 'Active' as 'Status',
            ActiveContractParkingCards.CardNo ,
            ActiveContractParkingCards.UserNo,
            ActiveContractParkingCards.UserSurname,
            ActiveContractParkingCards.CustomerNo,
            ActiveContractParkingCards.CustomerSurname,
            ActiveContractParkingCards.PlateNo,
            ActiveContractParkingCards.ArticleNo

My challenge is that the PlateNo field is a memo data type Nvarchar (MAX) and could potentially hold (9) different string values separated by a comma - see insert below showing 3 :-
My approach would be to duplicate each row 3 times for this example with a single PlateNo entry for each.

snapshot
0
Comment
Question by:John-S Pretorius
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:DcpKing
ID: 41785193
And your question is?  How to do this?
As a suggestion, accumulate the data first on the "local" server. Then handle any rows with plate numbers containing commas, and then transmit the data to the remote server.

Handling the plate number fields with commas?
Well, you could use an extra field, initially set to zero when the holding table is filled. Set it to the number of commas in each row's Plate Number field:
len(PlateNumber) - len(replace(PlateNumber, ',', '')

Open in new window

gives the number of commas!

Then go to each marked row, add the right number of extra rows, and parse out the text between the commas. There's an example in this EE answer.

Hope this helps

Mike
0
 

Author Comment

by:John-S Pretorius
ID: 41785272
Thank you Mike, I am actually overthinking it - what I need to do is export only the PlateNo column into a single .txt file with one plate number per line i.e.

WYX
XBX3465
EF9571
7BXXX09
7EXXX67
5XXXX52
WVR3833
VBE1748
....................
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 41785517
Ha!!! LOL.
Remember: All the best answers involve doing less!

Mike
0
 

Accepted Solution

by:
John-S Pretorius earned 0 total points
ID: 41791723
I have been digging and found below from the link : http://blog.sqlauthority.com/2015/04/21/sql-server-split-comma-separated-list-without-using-a-function/

Work's beautifull..

Thanks to Mr. Pinal

SELECT UserSurname,
LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS Certs
FROM
(
SELECT UserSurname,CAST('<XMLRoot><RowData>' + REPLACE(PlateNo,',','</RowData><RowData>') +
'</RowData></XMLRoot>' AS XML) AS x
FROM   dbo.SDContractParkers
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)
0
 

Author Closing Comment

by:John-S Pretorius
ID: 41797512
No one was able to resolve the challenge, I found a solution online.
0

Featured Post

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.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

879 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