Solved

MS SQL -- RegEx (or something)

Posted on 2012-03-16
10
517 Views
Last Modified: 2012-03-20
Change "select ExtensionSettings from Subscriptions"
so it only extracts "test3@test.com;test2@test.com"
on my 500+ results instead of getting the below ?

<ParameterValues><ParameterValue><Name>TO</Name><Value>test3@test.com;test2@test.com</Value></ParameterValue>
<ParameterValue><Name>ReplyTo</Name><Value>test@test.com</Value></ParameterValue>
<ParameterValue><Name>IncludeReport</Name><Value>True</Value></ParameterValue>
<ParameterValue><Name>RenderFormat</Name><Value>EXCEL</Value></ParameterValue>
<ParameterValue><Name>Subject</Name><Value>EOM -- @ReportName</Value>
0
Comment
Question by:finance_teacher
[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
10 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 37729142
select ExtensionSettings from Subscriptions where ExtensionSettings = 'test3@test.com;test2@test.com'
0
 

Author Comment

by:finance_teacher
ID: 37729267
That get the entire ExtensionSettings column, I only want the a piece of it.

Like a SUBSTRING, but based on a REGEX (or something else).
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37731669
Why not use Xml Data Type Methods for this?  That would be far more precise than using something like PATINDEX()
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:finance_teacher
ID: 37733568
I read http://learnsqlserver.in/6/Xml-Data-Type-Methods.aspx, but have issues.

Please post an example solution.

Thanks
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37733787
Here you go:
DECLARE @MyXml xml =
'<ParameterValues>
	<ParameterValue>
		<Name>TO</Name>
		<Value>test3@test.com;test2@test.com</Value>
	</ParameterValue>
	<ParameterValue>
		<Name>ReplyTo</Name>
		<Value>test@test.com</Value>
	</ParameterValue>
	<ParameterValue>
		<Name>IncludeReport</Name>
		<Value>True</Value>
	</ParameterValue>
	<ParameterValue>
		<Name>RenderFormat</Name>
		<Value>EXCEL</Value>
	</ParameterValue>
	<ParameterValue>
		<Name>Subject</Name>
		<Value>EOM -- @ReportName</Value>
	</ParameterValue>
</ParameterValues>'

SELECT	t.c.value('Value[1]', 'varchar(50)')
FROM	@MyXml.nodes('ParameterValues/ParameterValue') T(C)

Open in new window

0
 

Author Comment

by:finance_teacher
ID: 37733955
My database table, subscriptions, has 500+ rows, how can I get your "DECLARE @MyXml xml..." to do something like "DECLARE @MyXml xml...select ExtensionSettings from Subscriptions..." ?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37736105
Like this:
SELECT  s.*,
        t.c.value('Value[1]', 'varchar(50)') ExtensionSettings
FROM    subscriptions s
        CROSS APPLY s.MyXml.nodes('ParameterValues/ParameterValue') T (C)

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 400 total points
ID: 37736122
This is how I tested it:

DECLARE @subscriptions TABLE (MyXml xml)
INSERT @subscriptions (MyXml) VALUES (
'<ParameterValues>
	<ParameterValue>
		<Name>TO</Name>
		<Value>test3@test.com;test2@test.com</Value>
	</ParameterValue>
	<ParameterValue>
		<Name>ReplyTo</Name>
		<Value>test@test.com</Value>
	</ParameterValue>
	<ParameterValue>
		<Name>IncludeReport</Name>
		<Value>True</Value>
	</ParameterValue>
	<ParameterValue>
		<Name>RenderFormat</Name>
		<Value>EXCEL</Value>
	</ParameterValue>
	<ParameterValue>
		<Name>Subject</Name>
		<Value>EOM -- @ReportName</Value>
	</ParameterValue>
</ParameterValues>')

SELECT  s.*,
        t.c.value('Value[1]', 'varchar(50)') ExtensionSettings
FROM    @subscriptions s
        CROSS APPLY s.MyXml.nodes('ParameterValues/ParameterValue') T (C)

Open in new window

0
 
LVL 12

Assisted Solution

by:kselvia
kselvia earned 100 total points
ID: 37739584
Credit to ACPerkins, but here is some code that should work with your SQL Report Server subscriptions table;

SELECT  Description, t.c.value('Value[1]', 'varchar(50)') ExtensionSettings        
FROM    
( select CAST(extensionsettings AS XML) ExtensionSettings, Description from subscriptions ) s
CROSS APPLY s.ExtensionSettings.nodes('/ParameterValues/ParameterValue') T (C)
WHERE t.c.value('Name[1]', 'varchar(50)') = 'TO'

Open in new window

0
 

Author Comment

by:finance_teacher
ID: 37741846
Ok, I changed the below from "varchar(50)" to "varchar(500)" and it works.

SELECT  t.c.value('Value[1]', 'varchar(500)') ExtensionSettings        
FROM    
( select CAST(extensionsettings AS XML) ExtensionSettings, Description from subscriptions ) s
CROSS APPLY s.ExtensionSettings.nodes('/ParameterValues/ParameterValue') T (C)
WHERE t.c.value('Name[1]', 'varchar(500)') = 'TO'
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

691 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