SORTKEY function [String]

Function

Generates values that can be used to sort character strings based on alternate collation rules.

Syntax

SORTKEY ( string-expression [ , collation-name ] )

Parameters

string-expression The string expression may contain only characters that are encoded in the database’s character set. String expression is usually one of the table fields upon which users want the query results to be ordered.

If string-expression is an empty string, SORTKEY returns a zero-length binary value. If string-expression is NULL, SORTKEY returns a null value. An empty string has a different sort order value than a null string from a database column.

The maximum length of the string that SORTKEY can handle is 254 bytes. Any longer part is ignored.

collation-name A string or character variable that specifies the name of the sort order to use.

If you do not specify a collation, the default is “thaidict”. Although the following are all valid values for collation name, Sybase IQ is certified only with “thaidict”.

Table 5-18: Collation name for sort order

Sort order description

Collation name

Binary sort

binary

Default Unicode multilingual

default

CP850 Alternative: no accent

altnoacc

CP850 Alternative: lowercase first

altdict

CP850 Alternative: no case preference

altnocsp

CP850 Scandinavian dictionary

scandict

CP850 Scandinavian: no case preference

scannocp

Latin-1 English, French, German dictionary

dict

Latin-1 English, French German no case

nocase

Latin-1 English, French German no case preference

nocasep

Latin-1 English, French German no accent

noaccent

Latin-1 Spanish dictionary

espdict

Latin-1 Spanish no case

espnocs

Latin-1 Spanish no accent

espnoac

Thai dictionary

thaidict

Examples

Assume the following table schema:

CREATE TABLE T1(id int, c1 varchar(40) shadowc1 varbinary(240))

SORTKEY() returns values in the sort order thaidict (Thai dictionary), the Thai character set in UTF8 form. The following statements generate the same result:

SELECT c1, SORTKEY(c1) from T1 where rid=3
SELECT c1, SORTKEY(c1, ‘thaidict’) from T1 where rid=3)
SELECT ‘\340\270\201\340\271\207’,SORTKEY(‘\340\279\201\340\271\207’) from T1 where rid=3

NoteSybase IQ provides a utility to convert data files in CP874 format into UTF8 collation. For details, see “CP874toUTF8 utility” in Chapter 3, “Database Administration Utilities” in the Sybase IQ Utility Guide.

The following table shows SORTKEY results using thaidict:

c1 in ascii

c1 in binary

SORTKEY()

à, à1

\340\270\201\340\271\207

0x11a3011c

Usage

The following statements generate the same result. SORTKEY() returns values in the sort order default Unicode multilingual:

SELECT c1, SORTKEY(c1, ‘dict’) from T1 where rid=3
SELECT ‘coop’, SORTKEY(‘coop’, ‘dict’) from T1 where rid=3

The following table shows SORTKEY results using dict:

c1

SORTKEY()

0x08890997099709b30008000800080008

0x11a3011c

The SORTKEY function generates values that can be used to order results based on predefined sort order behavior. This allows you to work with character sort order behaviors that are beyond the limitation of collations supported by Sybase IQ. The returned value is a binary value that contains coded sort order information for the input string retained from the SORTKEY function.

For example, you can issue the following SELECT statement to retrieve data from table T1 in the sorted order of c1 according to the Thai dictionary:

SELECT rid, c1 from T1 ORDER BY SORTKEY(c1)

You could instead store the value returned by SORTKEY in a column with the source character string. When you need to retrieve the character data in the desired order, the SELECT statement needs to include only an ORDER BY clause on the column that contains the results of running SORTKEY.

UPDATE T1 SET shadowc1=SORTKEY(c1) FROM T1;
SELECT rid, c1 FROM T1 ORDER BY shadowc1

The SORTKEY function guarantees that the values it returns for a given set of sort order criteria work for the binary comparisons that are performed on varbinary data types.

The input of SORTKEY can generate up to six bytes of sort order information for each input character. The output of SORTKEY is of type varbinary and has a maximum length of (254 * 6) bytes.

Standards and compatibility

See also

Chapter 11, “International Languages and Character Sets” in Sybase IQ System Administration Guide