䯄婘嘉䘞饡釕 > 昄扞康 > PostgreSQL > PostgreSQL庖严婾庘昄

PostgreSQL庖严婾庘昄

準溊寘录桺䆹    䫌 悕垵幥 敘桄䬽橸    敟蓽庺渇

PostgreSQL䔇庖严婾庘昄婂襕䫘庯庖严婾淉嘩㔗婋臘臥䂖傋䂉庖麉襕䔇庖严婾庘昄 gitbook.net

劉䓄 柟誄
ASCII() 誫啂橔噥膹䔇庖严䔇昄唚
BIT_LENGTH() 誫啂埗昄䔇阪庥嘉
CHAR_LENGTH() 誫啂埗昄婺䔇庖严昄
CHARACTER_LENGTH() A synonym for CHAR_LENGTH()
CONCAT_WS() Return concatenate with separator
CONCAT() Return concatenated string
LCASE() Synonym for LOWER()
LEFT() Return the leftmost number of characters as specified
LENGTH() Return the length of a string in bytes
LOWER() Return the argument in lowercase
LPAD() Return the string argument, left-padded with the specified string
LTRIM() Remove leading spaces
MID() Return a substring starting from the specified position
POSITION() A synonym for LOCATE()
QUOTE() Escape the argument for use in an SQL statement
REGEXP Pattern matching using regular expressions
REPEAT() Repeat a string the specified number of times
REPLACE() Replace occurrences of a specified string
REVERSE() Reverse the characters in a string
RIGHT() Return the specified rightmost number of characters
RPAD() Append string the specified number of times
RTRIM() Remove trailing spaces
SUBSTRING(), SUBSTR() Return the substring as specified
TRIM() Remove leading and trailing spaces
UCASE() Synonym for UPPER()
UPPER() Convert to uppercase

ASCII(str)

Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the empty string. Returns NULL if str is NULL. ASCII() works for characters with numeric values from 0 to 255. www.gitbook.net

testdb=# SELECT ASCII('2');
+---------------------------------------------------------+
| ASCII('2')                                              |
+---------------------------------------------------------+
| 50                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec)

testdb=# SELECT ASCII('dx');
+---------------------------------------------------------+
| ASCII('dx')                                             |
+---------------------------------------------------------+
| 100                                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec) 

gitbook.net

BIT_LENGTH(str)

誫啂嘉庖严婾str䔇阪庥㔗

www.gitbook.net

testdb=# SELECT BIT_LENGTH('text');
+---------------------------------------------------------+
| BIT_LENGTH('text')                                      |
+---------------------------------------------------------+
| 32                                                      |
+---------------------------------------------------------+
1 row in set (0.00 sec) www.gitbook.net 

CHAR_LENGTH(str)

Returns the length of the string str, measured in characters. A multi-byte character counts as a single character. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5. gitbook.net

testdb=# SELECT CHAR_LENGTH('text');
+---------------------------------------------------------+
| CHAR_LENGTH('text')                                     |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
gitbook.net

CHARACTER_LENGTH(str)

CHARACTER_LENGTH() is a synonym for CHAR_LENGTH(). gitbook.net

CONCAT(str1,str2,...)

Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are non-binary strings, the result is a non-binary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent binary string form; if you want to avoid that, you can use an explicit type cast, as in this example:

gitbook.net

testdb=# SELECT CONCAT('My', 'S', 'QL');
+---------------------------------------------------------+
| CONCAT('My', 'S', 'QL')                                 |
+---------------------------------------------------------+
| MySQL                                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec) 
www.gitbook.net

CONCAT_WS(separator,str1,str2,...)

CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL. www.gitbook.net

testdb=# SELECT CONCAT_WS(',','First name','Last Name' );
+---------------------------------------------------------+
| CONCAT_WS(',','First name','Last Name' )                |
+---------------------------------------------------------+
| First name, Last Name                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec) gitbook.net 

LCASE(str)

LCASE() is a synonym for LOWER(). gitbook.net

LEFT(str,len)

Returns the leftmost len characters from the string str, or NULL if any argument is NULL. gitbook.net

testdb=# SELECT LEFT('foobarbar', 5);
+---------------------------------------------------------+
| LEFT('foobarbar', 5)                                    |
+---------------------------------------------------------+
| fooba                                                   |
+---------------------------------------------------------+
1 row in set (0.00 sec) gitbook.net 

LENGTH(str)

Returns the length of the string str, measured in bytes. A multi-byte character counts as multiple bytes. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5. gitbook.net

testdb=# SELECT LENGTH('text');
+---------------------------------------------------------+
| LENGTH('text')                                          |
+---------------------------------------------------------+
| 4                                                       |
+---------------------------------------------------------+
1 row in set (0.00 sec) 

www.gitbook.net

LOWER(str)

Returns the string str with all characters changed to lowercase according to the current character set mapping. www.gitbook.net

testdb=# SELECT LOWER('QUADRATICALLY');
+---------------------------------------------------------+
| LOWER('QUADRATICALLY')                                  |
+---------------------------------------------------------+
| quadratically                                           |
+---------------------------------------------------------+
1 row in set (0.00 sec) 

gitbook.net

LPAD(str,len,padstr)

Returns the string str, left-padded with the string padstr to a length of len characters. If str is longer than len, the return value is shortened to len characters.

gitbook.net

testdb=# SELECT LPAD('hi',4,'??');
+---------------------------------------------------------+
| LPAD('hi',4,'??')                                       |
+---------------------------------------------------------+
| ??hi                                                    |
+---------------------------------------------------------+
1 row in set (0.00 sec) 

www.gitbook.net

LTRIM(str)

Returns the string str with leading space characters removed. gitbook.net

testdb=# SELECT LTRIM('  barbar');
+---------------------------------------------------------+
| LTRIM('  barbar')                                       |
+---------------------------------------------------------+
| barbar                                                  |
+---------------------------------------------------------+
1 row in set (0.00 sec) gitbook.net 

MID(str,pos,len)

MID(str,pos,len) is a synonym for SUBSTRING(str,pos,len).

gitbook.net

POSITION(substr IN str)

POSITION(substr IN str) is a synonym for LOCATE(substr,str). www.gitbook.net

QUOTE_IDENT(string text), QUOTE_LITERAL(string text), QUOTE_LITERAL(value anyelement), QUOTE_NULLABLE(value anyelement)

All these functions return the given string suitably quoted to be used as an identifier in an SQL statement string. In the function QUOTE_IDENT, Quotes are added only if necessary. In function QUOTE_LITERAL, embedded single-quotes and backslashes are properly doubled. If a value is passed, coerce the given value to text and then quote it as a literal. The function QUOTE_NULLABLE, coerces the given value to text and then quote it as a literal; or, if the argument is null, return NULL. gitbook.net

Following are the examples for all these functions: www.gitbook.net

testdb=# SELECT QUOTE_IDENT('Foo bar');
 quote_ident
-------------
 "Foo bar"
(1 row)


testdb=# SELECT QUOTE_LITERAL(E'O\'Reilly');
 quote_literal
---------------
 'O''Reilly'
(1 row)


testdb=# SELECT QUOTE_LITERAL(42.5);
 quote_literal
---------------
 '42.5'
(1 row)


testdb=# SELECT QUOTE_NULLABLE(42.5);
 quote_nullable
----------------
 '42.5'
(1 row) www.gitbook.net 

expr REGEXP pattern

REGEXP_MATCHES(string text, pattern text [, flags text]) function performs a pattern match of expr against pattern. Returns 1 if expr matches pat; otherwise it returns 0. If either expr or pat is NULL, the result is NULL. REGEXP_MATCHES is not case sensitive, except when used with binary strings.

www.gitbook.net

REGEXP_REPLACE(string text, pattern text, replacement text [, flags text]) function replaces substring(s) matching a POSIX regular expression.

www.gitbook.net

REGEXP_SPLIT_TO_ARRAY(string text, pattern text [, flags text ]), Split string using a POSIX regular expression as the delimiter. gitbook.net

REGEXP_SPLIT_TO_TABLE(string text, pattern text [, flags text]), splits string using a POSIX regular expression as the delimiter.

gitbook.net

Following are the examples for all these functions: www.gitbook.net

testdb=# SELECT REGEXP_MATCHES('ABCDEF' ,'A%C%%');
 regexp_matches
----------------
(0 rows)


testdb=# SELECT REGEXP_REPLACE('Thomas', '.[mN]a.', 'M');
 regexp_replace
----------------
 ThM
(1 row)


testdb=# SELECT REGEXP_SPLIT_TO_ARRAY('hello world', E'\\s+');
 regexp_split_to_array
-----------------------
 {hello,world}
(1 row)


testdb=# SELECT REGEXP_SPLIT_TO_TABLE('hello world', E'\\s+');
 regexp_split_to_table
-----------------------
 hello
 world
(2 rows) gitbook.net 

REPEAT(str,count)

Returns a string consisting of the string str repeated count times. If count is less than 1, returns an empty string. Returns NULL if str or count are NULL.

www.gitbook.net

testdb=# SELECT REPEAT('SQL', 3);
  repeat
-----------
 SQLSQLSQL
(1 row) 
gitbook.net

REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str. www.gitbook.net

testdb=# SELECT REPLACE('www.mysql.com', 'w', 'Ww');
     replace
------------------
 WwWwWw.mysql.com
(1 row) gitbook.net 

REVERSE(str)

Returns the string str with the order of the characters reversed.

www.gitbook.net

testdb=# SELECT REVERSE('abcd');
 reverse
---------
 dcba
(1 row) 

gitbook.net

RIGHT(str,len)

Returns the rightmost len characters from the string str, or NULL if any argument is NULL. gitbook.net

testdb=# SELECT RIGHT('foobarbar', 4);
 right
-------
 rbar
(1 row) gitbook.net 

RPAD(str,len,padstr)

Returns the string str, right-padded with the string padstr to a length of len characters. If str is longer than len, the return value is shortened to len characters.

gitbook.net

testdb=# SELECT RPAD('hi',5,'?');
 rpad
-------
 hi???
(1 row) 
gitbook.net

RTRIM(str)

Returns the string str with trailing space characters removed.

gitbook.net

testdb=# SELECT RTRIM('barbar   ');
 rtrim
--------
 barbar
(1 row) 

www.gitbook.net

SUBSTRING(str,pos)

SUBSTRING(str FROM pos)

SUBSTRING(str,pos,len)

SUBSTRING(str FROM pos FOR len)

The forms without a len argument return a substring from string str starting at position pos. The forms with a len argument return a substring len characters long from string str, starting at position pos. The forms that use FROM are standard SQL syntax. It is also possible to use a negative value for pos. In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning. A negative value may be used for pos in any of the forms of this function.

gitbook.net

testdb=# SELECT SUBSTRING('Quadratically',5);
 substring
-----------
 ratically
(1 row)


testdb=# SELECT SUBSTRING('foobarbar' FROM 4);
 substring
-----------
 barbar
(1 row)


testdb=# SELECT SUBSTRING('Quadratically',5,6);
 substring
-----------
 ratica
(1 row) www.gitbook.net 

TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)

TRIM([remstr FROM] str)

Returns the string str with all remstr prefixes or suffixes removed. If none of the specifiers BOTH, LEADING, or TRAILING is given, BOTH is assumed. remstr is optional and, if not specified, spaces are removed.

gitbook.net

testdb=# SELECT TRIM('  bar   ');
 btrim
-------
 bar
(1 row)


testdb=# SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');
 ltrim
--------
 barxxx
(1 row)


testdb=# SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');
 btrim
-------
 bar
(1 row)


testdb=# SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
 rtrim
-------
 bar
(1 row) 

www.gitbook.net

UCASE(str)

UCASE() is a synonym for UPPER(). gitbook.net

UPPER(str)

Returns the string str with all characters changed to uppercase according to the current character set mapping.

www.gitbook.net

testdb=# SELECT UPPER('manisha');
  upper
---------
 MANISHA
(1 row) gitbook.net 
橸䆍桺䆹鍴濘滯蘸蘘崡庺婺橸䆍寘录潡䚡臏
渵誯傂嘘嘵嚟䔇蘸蘘嘖臙媇媙濘滯庺崇優麉傡庺媿媘嚹携庥幹嘺䇔昍䘋
蘸蘘臙濘滯桺䆹蘸蘘躻悕垵幥 [http://www.gitbook.net]
橸桺湺鵻PostgreSQL庖严婾庘昄
蘸蘘臙媺䘍寘桺鷆毖:http://www.gitbook.net/html/postgresql/2013/080893.html