How cast in XML for aggregate functions

Posted by renegm on Stack Overflow See other posts from Stack Overflow or by renegm
Published on 2010-12-23T17:11:32Z Indexed on 2010/12/23 19:54 UTC
Read the original article Hit count: 217

Filed under:
|
|

In SQL Server 2008.

I need execute a query like that:

DECLARE @x AS xml
SET @x=N'<r><c>First Text</c></r><r><c>Other Text</c></r>'
SELECT @x.query('fn:max(r/c)')

But return nothing (apparently because convert xdt:untypedAtomic to numeric)

How to "cast" r/c to varchar?

Something like

SELECT @x.query('fn:max(«CAST(r/c «AS varchar(20))»)')

Edit: Using Nodes the function MAX is from T-SQL no fn:max function In this code:

DECLARE @x xml;
SET @x = '';
SELECT @x.query('fn:max((1, 2))');
SELECT @x.query('fn:max(("First Text", "Other Text"))');

both query return expected: 2 and "Other Text" fn:max can evaluate string expression ad hoc. But the first query dont work. How to force string arguments to fn:max?

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about Xml