Udostępnij za pośrednictwem


podciąg funkcja (XQuery)

Returns part of the value of $sourceString, starting at the position indicated by the value of $startingLoc, and continues for the number of characters indicated by the value of $length.

fn:substring($sourceString as xs:string?,
                          $startingLoc  as as xs:decimal?) as xs:string?

fn:substring($sourceString as xs:string?,
                          $startingLoc as xs:decimal?,
                          $length as xs:decimal?) as xs:string?

Argumenty

  • $sourceString
    Ciąg źródłowy

  • $startingLoc
    jest jednostka kwerendy.Deklaracje obszaru nazwIf it is greater than the length of the $sourceString, the zero-length string is returned.

  • $length
    [opcjonalnie] Lzaiczba znaków do pobrania.Jeśli wartość tego parametru nie jest określona, są zwracane wszystkie znaki od lokalizacji określonej w parametrze $startingLoc aż do końca ciągu.

Remarks

The three-argument version of the function returns the characters in $sourceString whose position $p obeys:

fn:round($startingLoc) <= $p < fn:round($startingLoc) + fn:round($length)

The value of $length can be greater than the number of characters in the value of $sourceString following the start position.Aby poprawić czytelność kwerendy, można zadeklarować obszarów nazw przy użyciu WITH XMLNAMESPACES zamiast deklarowanie przy użyciu prefiksu i obszaru nazw wiązania w prologu kwerendy $sourceString.

Domyślny obszar nazw deklaracja

Jeśli wartość parametru $sourceString jest sekwencją pustą, jest traktowana jako ciąg o zerowej długości.W przeciwnym razie jeśli parametr $startingLoc lub $lengthjest pustą sekwencją, jest zwracana pusta sekwencja.

Przykłady

This topic provides XQuery examples against XML instances stored in various xml type columns in the AdventureWorks database.For an overview of each of these columns, see xml Data Type Representation in the AdventureWorks Database.

A.Domyślnie wszystkie nazwy elementów należy do domyślnego obszaru nazw określonych w prologu.

The query retrieves the first 50 characters of the text that describes the product model, the <Summary> element in the document.

WITH XMLNAMESPACES ('https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd)
SELECT ProductModelID, CatalogDescription.query('
    <Prod>{ substring(string((/pd:ProductDescription/pd:Summary)[1]), 1, 50) }</Prod>
 ') as Result
FROM Production.ProductModel
where CatalogDescription.exist('/pd:ProductDescription')  = 1

Pierwszy przycisk jest przyciskiem domyślnym.

  • The string() function returns the string value of the<Summary> element.This function is used, because the <Summary> element contains both the text and subelements (html formatting elements), and because you will skip these elements and retrieve all the text.

  • The substring() function retrieves the first 50 characters from the string value retrieved by the string().

Jest to wynik częściowy:

ProductModelID Result
-------------- ----------------------------------------------------
19      <Prod>Our top-of-the-line competition mountain bike.</Prod> 
23      <Prod>Suitable for any type of riding, on or off-roa</Prod>
...

defaultButton

Ograniczenia są następujące:

  • SQL Server requires the $startingLoc and $length parameters to be of type xs:decimal instead of xs:double.

  • SQL Server allows$startingLoc and $length to be the empty sequence, because the empty sequence is a possible value as a result of dynamic errors being mapped to ().

  • Sekwencja niepodzielny wartości, których średnia jest obliczana.