Solved

SQL Query mechanasism needs attention

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

617 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