May 1, 2010 at 11:40 pm
I am doing some good old string concentatenation, and I've been reading up on using for XML Paths commands to do it.
Here is some sample code I've used.
SELECT documentID
,(SELECT @DELIMIT + [VALUE] FROM
(SELECT DOC.documentID,CONT.ContentKey as FIELDNAME,CONT.CONTENTVALUE as VALUE
FROM dbo.Document as DOC
inner join dbo.DocumentContent as CONT
on CONT.documentID = DOC.documentID
AND DOC.PROCESSID = @ID) as s2
WHERE s2.documentID= s1.documentID AND s2.FIELDNAME = S1.FIELDNAME
ORDER BY documentID
FOR XML PATH('')) as [VALUES]
,FIELDNAME
FROM
(SELECT DOC.documentID,CONT.ContentKey as FIELDNAME,CONT.CONTENTVALUE as VALUE
FROM dbo.Document as DOC
inner join dbo.DocumentContent as CONT
on CONT.documentID = DOC.documentID
AND DOC.PROCESSID = @ID) as s1
GROUP BY s1.documentID,FIELDNAME
The query is concentating my strings, however, it is also altering them. Here's an example:
<bob.bobson@bob.com>
BECOMES
&(lt);bob.bobson@bob.com&(gt)
*NOTE: The () are not actually in the text. I added them because the forum would not render my words properly without them.
Is this expected behavior for this command, or is there something extra I am not doing?
While there are other methods of concetating, this method so far has turned out to be the fastest so I'm hoping its fixable.
May 2, 2010 at 8:16 am
As to the "Why", check out this link.
One thing to note there is that you can utilize a processing-instruction (thanks Jason & Paul!). So, this code might help you out.
declare @test-2 TABLE (RowID int IDENTITY, Data varchar(200))
insert into @test-2
select 'some regular text' UNION ALL
select 'email: <bob.jones@domain.com>' UNION ALL
select 'Tom & Jerry'
select Data
from @test-2
FOR XML PATH('')
select Data as [processing-instruction(Data)]
from @test-2
FOR XML PATH('')
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 2, 2010 at 10:09 am
Here is a quick example (using WayneS sample code)
declare @test-2 TABLE (RowID int IDENTITY, Data varchar(200))
insert into @test-2
select 'some regular text' UNION ALL
select 'email: <bob.jones@domain.com>' UNION ALL
select 'Tom & Jerry'
select t.r.value('.[1]','varchar(512)')
from (select Data+' '
from @test-2
FOR XML PATH(''),type) t(r)
Here, I am making sure the concatenated string is returned as an xml fragment and then grabbing that as text into a varchar. This method eliminates the encoding problem you are seeing.
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
May 2, 2010 at 10:46 am
mister.magoo (5/2/2010)
Here is a quick example (using WayneS sample code)
declare @test-2 TABLE (RowID int IDENTITY, Data varchar(200))
insert into @test-2
select 'some regular text' UNION ALL
select 'email: <bob.jones@domain.com>' UNION ALL
select 'Tom & Jerry'
select t.r.value('.[1]','varchar(512)')
from (select Data+' '
from @test-2
FOR XML PATH(''),type) t(r)
Here, I am making sure the concatenated string is returned as an xml fragment and then grabbing that as text into a varchar. This method eliminates the encoding problem you are seeing.
Thanks for the quick help guys. Mister.Magoo, I'm a bit confused on this statement: (t.r.value('.[1]','varchar(512)'))
What is going on with this command, for example what does the .[1] do?
May 2, 2010 at 11:02 am
In the code fragment
t.r.value('.[1]','varchar(512)')
".[1]" retrieves the first node in t.r
and
value('.[1]','varchar(512)')
converts that node's value to a varchar(512).
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
May 2, 2010 at 12:19 pm
mister.magoo (5/2/2010)
Here is a quick example (using WayneS sample code)
declare @test-2 TABLE (RowID int IDENTITY, Data varchar(200))
insert into @test-2
select 'some regular text' UNION ALL
select 'email: <bob.jones@domain.com>' UNION ALL
select 'Tom & Jerry'
select t.r.value('.[1]','varchar(512)')
from (select Data+' '
from @test-2
FOR XML PATH(''),type) t(r)
Here, I am making sure the concatenated string is returned as an xml fragment and then grabbing that as text into a varchar. This method eliminates the encoding problem you are seeing.
Well done! I'm going to have to remember this myself!
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 3, 2010 at 1:11 pm
Unfortunately another snag has kicked in. I finally got the code to work but when I apply it to the actual data I get the following error:
Cannot call methods on nvarchar(max).
It looks like the value call cannot be applied to these types of fields, which unfortunately is exactly the fields I need to apply this on.
Any other suggestions? I could try a replace command to fix the descrepencies but the speed benefits for this method may fade with replace, I'll have to do some testing to see.
May 3, 2010 at 4:18 pm
It sounds like you have missed the "type" part of the for xml path('') statement...
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
May 4, 2010 at 7:33 pm
mister.magoo (5/3/2010)
It sounds like you have missed the "type" part of the for xml path('') statement...
Quite right sir, when I made the correction it worked beautifully. Does this command perform any other data alteration I need to be aware of, does it handle unicode, etc.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply