Solved

SQL Query mechanasism needs attention

Posted on 2016-09-05
5
45 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
[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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table where row act as column 11 69
removing rows from sql result set  where some column values are repeated 1 25
Need help with another query 10 37
SQL profiler 3 14
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

749 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