Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Bob's little blog with a focus on SQL Server and XML as we go [echoing voice] "Beyond Relational!"
Browse by Tags · View All
BRH 2
XML 2
#TSQL 1
TSQL 1
#SQLServer 1
SQL Server 1
parse delimited string 1
list type 1
MDS 1
SQL Server 2008 R2 1

Archive · View All
February 2011 1
May 2010 1

Parsing space-delimited strings to columns

Feb 17 2011 5:35PM by wBob   

I learned a new technique recently for shredding delimited strings.  It uses the xsd list type and SQL Server's xml data type and is quite powerful.  It's a bit different to most shredding techniques in that it allows easy shredding to columns (instead of rows).

 

USE tempdb
GO

CREATE XML SCHEMA COLLECTION testListSchema AS
'<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

 <xs:simpleType name="longintegerlist">
  <xs:list itemType="xs:long"/>
 </xs:simpleType>

 <xs:element name="Root">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="i" type="longintegerlist" />
   </xs:sequence>
  </xs:complexType>
 </xs:element>

</xs:schema>'
GO

DECLARE @t TABLE ( xCol XML(testListSchema) )

INSERT INTO @t VALUES ( '<Root>
<i>1 2 10 4</i>
</Root>')

SELECT 
 xCol.value('data(Root/i)[1]', 'INT'),
 xCol.value('data(Root/i)[2]', 'INT'),
 xCol.value('data(Root/i)[3]', 'INT'),
 xCol.value('data(Root/i)[4]', 'INT')
FROM @t
GO

-- Cleanup
IF EXISTS( SELECT * FROM sys.xml_schema_collections WHERE name = 'testListSchema' )
DROP XML SCHEMA COLLECTION testListSchema
GO

The technique does rely on the string being space delimited, but you could always use the SQL REPLACE function to clean the string before shredding.  The beauty of the technique is that it's actually quite simple, if you can get past the XSD list type.  So this is handy for shredding delimited strings when you know the number of elements you want to parse out and you want to parse to columns.

I hope you enjoyed our trip as we went beyond relational, parsing out delimited strings into columns using XML!

Tags: XML, BRH, list type, parse delimited string, SQL Server, #SQLServer, TSQL, #TSQL,


wBob
333 · 0% · 124
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • Very interesting technique, Bob. Thanks for sharing this.

    commented on Feb 18 2011 7:18AM
    JacobSebastian
    47 · 4% · 1215
  • Nice, thanks

    commented on Feb 28 2011 9:59AM
    Naomi
    33 · 6% · 1774

Your Comment


Sign Up or Login to post a comment.

"Parsing space-delimited strings to columns" rated 5 out of 5 by 1 readers
Parsing space-delimited strings to columns , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]