Easiest way to query a SQL Server 2008 R2 XML data type? - sql

I need to get a node value in an XML data type column.
<CustomContentData>
<prpIsRSSFeed>false</prpIsRSSFeed>
</CustomContentData>
How is this done in SQL Server?
The column name is ClassXML

Use XQuery, a simple example with your data would be:
DECLARE #T TABLE (ClassXML XML);
INSERT #T (ClassXML)
VALUES ('<CustomContentData>
<prpIsRSSFeed>false</prpIsRSSFeed>
</CustomContentData>');
SELECT t.ClassXML.value('CustomContentData[1]/prpIsRSSFeed[1]', 'VARCHAR(5)')
FROM #T AS t;

If the column is already XML data type in SQL Server, then the code below should work by using the value function with XPATH. If it's stored as a varchar, you'd just need to replace ClassXML.value with CONVERT(XML, ClassXML).value. Hope this helps!
DECLARE #Data TABLE (ClassXML XML)
INSERT #Data VALUES ('<CustomContentData><prpIsRSSFeed>false</prpIsRSSFeed></CustomContentData>')
SELECT
CONVERT(BIT, CASE WHEN ClassXML.value ('(/CustomContentData/prpIsRSSFeed)[1]',
'VARCHAR(50)') = 'true' THEN 1 ELSE 0 END) AS IsRssFeed
FROM #Data
Yields output
IsRssFeed
---------
0

Related

SQL Server Conditional Count Issue

I am trying to do something apparently simple in sql server but not been able to achieve the desired result yet (I am of course not a sql expert).
My source table:
And I am trying to get the output like below:
I have tried to give the field names meaningful so that the problem becomes self explanatory. I haven't been able to generate the 3rd column of the desired output yet.
Please can someone help??
Thanks & Regards.
You can try below -
select date,count(*) as idcount,count(case when status='Pass' then 1 end) as idpassed
from tablname
group by date
Try this:
Declare #t table( dates varchar(50),id int,status varchar(50))
insert into #t values ('2019/8',1,'Pass')
insert into #t values ('2019/9',1,'fail')
insert into #t values ('2019/9',2,'fail')
insert into #t values ('2019/8',3,'fail')
select dates,count(id) idcount,sum(case when status='pass' then 1 else 0 end) as pascount from #t
group by dates

Select SQL Query to get xml node values from ntext column?

I want to get one xml node value from NTEXT column which contains xml based on where clause quering on another xml node value.
RDBMS Type: Microsoft SQL Server T-SQL
Here: I want to get Code node value based on StoreId where clause value. How do I get it?
Input: 100
Output:ABCDE
For example:
<root>
<StoreProfile>
<General>
<StoreId>100</StoreId>
<Code>ABCDE</Code>
</General>
</StoreProfile>
</root>
If you are using SQL Server 2005 or 2008 you can use XQuery like so:
For more on XQuery see XQuery Language Reference
DECLARE #storeId INT
SET #storeId = 100
CREATE TABLE #TestTable
(
xmlColumn NTEXT
)
INSERT INTO #TestTable (xmlColumn) Values('<root><StoreProfile><General><StoreId>100</StoreId><Code>ABCDE</Code></General></StoreProfile></root>')
INSERT INTO #TestTable (xmlColumn) Values('<root><StoreProfile><General><StoreId>200</StoreId><Code>FGHIJ</Code></General></StoreProfile></root>')
SELECT
StoreProfile.value('Code[1]', 'nvarchar(10)') as Code
FROM #TestTable
CROSS APPLY (SELECT CAST(xmlColumn AS XML)) AS A(B)
CROSS APPLY A.B.nodes('//root/StoreProfile/General[StoreId = sql:variable("#storeId")]') AS StoreProfiles(StoreProfile)
DROP TABLE #TestTable

SQL Server 2005: How to perform a split on a string

I have the following string that I need to split from a field called symbols
234|23|HC
This is my current SQL statement
declare #t xml;
Set #t = (
Select symbols from tc for xml auto, elements)
Select #t;
which produces <symbols>234|23|HC</symbols>
but I need to split the string into child nodes so the result is like this:
<symbols>
<symbol>234</symbol>
<symbol>23</symbol>
<symbol>HC</symbol>
</symbols>
A replace version that takes care of the problem characters.
declare #T table(symbol varchar(50))
insert into #T values ('234|23|HC|Some problem chars <> &')
select cast('<symbols><symbol>'+
replace(cast(cast('' as xml).query('sql:column("symbol")') as varchar(max)),
'|',
'</symbol><symbol>')+
'</symbol></symbols> ' as xml)
from #T
Result:
<symbols>
<symbol>234</symbol>
<symbol>23</symbol>
<symbol>HC</symbol>
<symbol>Some problem chars <> &</symbol>
</symbols>

how to get values inside an xml column, when it's of type nvarchar

My question is similar to this one: Choose a XML node in SQL Server based on max value of a child element
except that my column is NOT of type XML, it's of type nvarchar(max).
I want to extract the XML node values from a column that looks like this:
<Data>
<el1>1234</el1>
<el2>Something</el2>
</Data>
How can I extract the values '1234' and 'Something' ?
doing a convert and using the col.nodes is not working.
CONVERT(XML, table1.col1).value('(/Data/el1)[1]','int') as 'xcol1',
After that, I would like to do a compare value of el1 (1234) with another column, and update update el1 as is. Right now I'm trying to just rebuild the XML when passing the update:
ie
Update table set col1 ='<Data><el1>'+#col2+'</el1><el2>???</el2>
You've got to tell SQL Server the number of the node you're after, like:
(/Data/el1)[1]
^^^
Full example:
declare #t table (id int, col1 varchar(max))
insert #t values (1, '<Data><el1>1234</el1><el2>Something</el2></Data>')
select CAST(col1 as xml).value('(/Data/el1)[1]', 'int')
from #t
-->
1234
SQL Server provides a modify function to change XML columns. But I think you can only use it on columns with the xml type. Here's an example:
declare #q table (id int, col1 xml)
insert #q values (1, '<Data><el1>1234</el1><el2>Something</el2></Data>')
update #q
set col1.modify('replace value of (/Data/el1/text())[1] with "5678"')
select *
from #q
-->
<Data><el1>5678</el1><el2>Something</el2></Data>
At the end of the day, SQL Server's XML support makes simple things very hard. If you value maintainability, you're better off processing XML on the client side.

Serializing Hashset contents to a SQL table

I have a hashset that I want to serialize to a SQL Server table. When serialized hashset looks like this...
<InstallerContactIds>
<int>153771</int>
<int>209572</int>
</InstallerContactIds>
I am using the following to insert the XML into the table...
INSERT INTO dbo.cv_AssessorActionPlanInstallers
SELECT #AssessorActionPlanId, InstallerId
From OPENXML (#XmlDocumentHandle, 'AssessorActionPlan/InstallerContactIds', 2)
With
(
InstallerId int 'int'
)
However, I only get the first id inserted into the table. Is it possible to insert all ids in this case?
I am using SQL Server 2005
You didn't specify what version of SQL you are using, but if you are on 2005 or newer, you can use the new XML data type.
declare #xml xml
select #xml = '
<InstallerContactIds>
<int>153771</int>
<int>209572</int>
</InstallerContactIds>'
select
x.i.value('.', 'int')
from
#xml.nodes('/InstallerContactIds/int') as x(i)
If you are stuck using OpenXML, it should work if you do this:
INSERT INTO dbo.cv_AssessorActionPlanInstallers
SELECT #AssessorActionPlanId, InstallerId
From OPENXML (#XmlDocumentHandle, 'AssessorActionPlan/InstallerContactIds/int', 2)
With
(
InstallerId int '.'
)