Solved

For XML Path Alternative Method?

Posted on 2014-02-08
12
634 Views
Last Modified: 2014-02-24
Is there an alternative method to using the 'For XML Path' below?

Select  
SUBSTRING 
(
(
SELECT 
CONVERT(varchar(15),CAST(StartDate as TIME),100)
 + ' - ' + 
CONVERT(varchar(15),CAST(EndDate AS TIME),100)  + CHAR(10) as [text()]
FROM Table2 MD
WHERE (MD.id = Table1.id) FOR XML path(''), elements
), 1, 100
) 
as [Meeting Times]

From Table1

Open in new window

0
Comment
Question by:WorknHardr
  • 7
  • 4
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39844093
What are you trying to accomplish?
0
 

Author Comment

by:WorknHardr
ID: 39844399
The result is more clearly seen in SQL Report Builder 3.0 like so. I'm building MSSQL SP to load into the report dataset.

[Meeting Times]
01/01/2014 - 01/03/2014
02/07/2014 - 02/09/2014
03/14/2014 - 03/17/2014


Select  
SUBSTRING 
(
(
SELECT 
CONVERT(varchar(15),CAST(StartDate as TIME),100)
 + ' - ' + 
CONVERT(varchar(15),CAST(EndDate AS TIME),100)  + CHAR(10) as [text()]
FROM Table2 MD
WHERE (MD.id = Table1.id) FOR XML path(''), elements
), 1, 100
) 
as [Meeting Times]

From Table1

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39844610
There are a number of methods you can use.  For example you can use a scalar UDF to do this.  Let me know if you can use functions and if you need an example.
0
 

Author Comment

by:WorknHardr
ID: 39844629
I like reusable functions.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39844731
Please post the schema for your Table1 and Table2 and I will see what I can do.
0
 

Author Comment

by:WorknHardr
ID: 39852910
[Table1]
ID1    Name
1        Meeting Times

[Table2]
ID2    ID1   StartDate         EndDate
 1         1     01/01/2014     01/03/2014
 2         1     02/07/2014     02/09/2014
 3         1     03/14/2014     03/17/2014

[Meeting Times]
01/01/2014 - 01/03/2014
02/07/2014 - 02/09/2014
03/14/2014 - 03/17/2014

Open in new window

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39855158
So the schema for
Table 1
ID integer.
Name varchar(20)

Table 2
ID2 integer,
ID1 integer
StartDate datetime,
EndDate datetime

And they are both related by ID1.  Is that correct?
0
 

Author Comment

by:WorknHardr
ID: 39856796
Yes, indeed...
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 400 total points
ID: 39857835
Perhaps we have made it more complicated than it needs to be.  Why not just write the query like this:
SELECT	CONVERT(varchar(10), StartDate, 101) + '-' + CONVERT(varchar(10), EndDate, 101) [Meeting Times]
FROM	Table2

Open in new window


What am I missing?
0
 

Author Comment

by:WorknHardr
ID: 39857999
You got it.
I think the next question is:
Q. Why use 'FOR XML' in the first place?
0
 

Author Closing Comment

by:WorknHardr
ID: 39865433
thx
0
 

Author Comment

by:WorknHardr
ID: 39884091
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now