Select 2nd row in XML Column in database using SQL
Check it out below.
Assuming your db is MS SQL Server.
The XQuery .value()
method has two mandatory parameters.
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ModuleID INT PRIMARY KEY, XMLCOL XML);
INSERT INTO @tbl (ModuleID, XMLCOL) VALUES
(959, N'<ArrayOfActionResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ActionResult>
<ResultType>Redirected to Payment</ResultType>
<ActionName>Payment</ActionName>
<ExecutionTime></ExecutionTime>
<ConditionSet>
<Conditions/>
<ExecuteCondition>Always</ExecuteCondition>
<MatchCondition>All</MatchCondition>
<ExecuteStatus>0</ExecuteStatus>
<Groups/>
</ConditionSet>
<ConditionsMet>true</ConditionsMet>
<Condition/>
</ActionResult>
<ActionResult>
<ResultType>Success</ResultType>
<ActionName>Payment</ActionName>
<ExecutionTime></ExecutionTime>
<ConditionSet>
<Conditions/>
<ExecuteCondition>Always</ExecuteCondition>
<MatchCondition>All</MatchCondition>
<ExecuteStatus>0</ExecuteStatus>
<Groups/>
</ConditionSet>
<ConditionsMet>true</ConditionsMet>
</ActionResult>
</ArrayOfActionResult>');
-- DDL and sample data population, end
SELECT ModuleID
, XMLCOL.value('(/ArrayOfActionResult/ActionResult[2]/ResultType/text())[1]','VARCHAR(30)') as PaymentMessage
FROM @tbl
WHERE ModuleID = 959;
Output
+----------+----------------+
| ModuleID | PaymentMessage |
+----------+----------------+
| 959 | Success |
+----------+----------------+