Solved

For XML Path Alternative Method?

Posted on 2014-02-08
12
602 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 74

Expert Comment

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

Author Comment

by:WorknHardr
Comment Utility
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
Comment Utility
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
Comment Utility
I like reusable functions.
0
 
LVL 75

Expert Comment

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

Author Comment

by:WorknHardr
Comment Utility
[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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
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
Comment Utility
Yes, indeed...
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 400 total points
Comment Utility
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
Comment Utility
You got it.
I think the next question is:
Q. Why use 'FOR XML' in the first place?
0
 

Author Closing Comment

by:WorknHardr
Comment Utility
thx
0
 

Author Comment

by:WorknHardr
Comment Utility
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now