Solved

SQL Query mechanasism needs attention

Posted on 2016-09-05
5
46 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where clause to fliter varchar with Characters 12 59
SQL Server Database Inventory 7 28
Search Text in Views 2 28
how to use ROW_NUMBER() correctly 8 44
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 ?
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.
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.

739 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