Some string operation functions of the PostgreSQL database
The customer encountered a problem during the project today. Some data exists,ProgramAfter analysis, we found that there were several blank characters in front of the Data. Then we used SQL to query the data and found that the eight or nine data tables, nearly 3 million of tens of millions of data records have the same problem. I wanted to add the matching character '%' During the query. Then I ran the test run and found that it was not feasible, because there are still many other pages with similar search problems, which will greatly affect the query speed. In addition, the customer urgently needs to solve this problem, because the program needs to be accessed continuously during the day, in addition, it cannot have a great impact on the running speed. Therefore, the JDBC Modification Scheme and the modification program search scheme are excluded.CodeSolutions, headaches
Later, I thought about it carefully and tried to find the relevant SQL string operation function to confirm whether there is a quick way to solve the blank string problem. Soon, we found a suitable solution, I tested one of the test databases and the results were very satisfactory. More than 3 million of the data only took a few minutes, removing all the useless spaces. I was so excited... Usage:
Update property set memorial_no = btrim (memorial_no, '') Where memorial_no like '%'
Or
Update property set memorial_no = trim (both ''from memorial_no) Where memorial_no like '%'
The btrim () method is used to delete certain types of characters on both sides of a string. multiple types of characters can be specified at the same time. The value above is '', meaning space.
The TRIM () method can implement all the functions that btrim () can implement. In fact, btrim () is used to delete a string on both sides. Trim () can specify only one side, of course, you can also
The string operation functions of postgresql are listed for use by the Japanese side.
Function: String | string
Note: String concatenation string connection operation
Example: 'post' | 'gresql' = PostgreSQL
Function: String | non-string or non-string | string
Description: String concatenation with one non-string input string and non-string type.
Example: 'value: '| 42 = value: 42
Function: bit_length (string)
Description: Number of BITs in string calculates the number of digits of a string.
Example: bit_length ('job') = 32
Function: char_length (string) or character_length (string)
Description: number of characters in string calculates the number of characters in a string.
Example: char_length ('job') = 4
Function: lower (string)
Description: Convert string to lower case converts the string to lowercase.
Example: bit_length ('job') = 32
Function: octet_length (string)
Description: number of bytes in string calculates the number of bytes of a string.
Example: octet_length ('job') = 4
Function: overlay (string placing string from int [for int])
Note: replace substring replaces the substring of any length in the string with the new string.
Example: overlay ('txxxxas 'placing' from 2 for 4) = 4
Function: Position (substring in string)
Note: Location of specified position of the substring in a string
Example: Position ('om 'in 'Thomas') = 3
Function: substring (string [from int] [for int])
Description: extract substring intercepts substrings of any length.
Example: substring ('Thomas 'from 2 for 3) = hom
Function: substring (string from pattern)
Note: extract substring matching POSIX regular expression. See section 9.7 for more information on pattern matching. use regular expressions to intercept strings of any length.
Example: substring ('Thomas 'from'... $ ') = MAS
Function: substring (string from pattern for escape)
Note: extract substring matching SQL regular expression. See section 9.7 for more information on pattern matching. Regular expressions are used to delete certain types of characters to obtain substrings.
Example: trim (both 'X' from 'xtomxx') = Tom
Function: trim ([leading | trailing | both] [characters] From string)
Note: remove the longest string containing only the characters (a space by default) from the start/end/both ends of the string to remove as long as possible the start, end, or two types of characters, by default, spaces are removed. Of course, you can specify multiple strings to be deleted at the same time.
Example: trim (both 'X' from 'xtomxx') = Tom
Function: Upper (string)
Description: Convert string to uppercase converts a string to uppercase.
Example: Upper ('Tom ') = Tom
Function: ASCII (string)
Note: ASCII code of the first character of the argument. for utf8 returns the Unicode code point of the character. for other multibyte encodings. the argument must be a strictly ASCII character. obtains the asⅱ value of a character.
Example: ASCII ('x') = 120
Function: btrim (string text [, characters text])
Note: remove the longest string consisting only of characters in characters (a space by default) from the start and end of string removes all specified characters on both sides of the string. Multiple characters can be specified at the same time.
Example: btrim ('xyxtrimyyx ', 'xy') = trim
Function: CHR (INT)
Note: character with the given code. for utf8 the argument is treated as a unicode Code Point. for other multibyte encodings the argument must designate a strictly ASCII character. the null (0) character is not allowed because text data types cannot store such bytes. obtains the character corresponding to an acⅱ value.
Example: CHR (65) =
Function: Convert (string bytea, src_encoding name, dest_encoding name)
Note: Convert string to dest_encoding. the original encoding is specified by src_encoding. the string must be valid in this encoding. conversions can be defined by create conversion. also there are some predefined conversions. see Table 9-7 for available conversions. converts string encoding to specify the source and target encoding.
Example: Convert ('text _ in_utf8', 'utf8', 'latin1') = text_in_utf8 represented in ISO 8859-1 encoding
Function: convert_from (string bytea, src_encoding name)
Note: Convert string to the database encoding. the original encoding is specified by src_encoding. the string must be valid in this encoding. to convert the string encoding, you must specify the source encoding. The target encoding is the database encoding by default,
Example: convert_from ('text _ in_utf8', 'utf8') = text_in_utf8 represented in the current database Encoding
Function: convert_to (string text, dest_encoding name)
Note: Convert string to dest_encoding. Convert the string encoding. The source encoding specifies the encoding by default for the database. You must specify the target encoding,
Example: convert_to ('some text', 'utf8') = some text represented in the utf8 Encoding
Function: Decode (string text, type text)
Description: Decode binary data from string previusly encoded with encode. Parameter type is same as in encode. decodes strings of the specified type.
Example: Decode ('tizaae = ', 'base64') = 123 \ 000 \ 001
Function: encode (Data bytea, type text)
Note: encode binary data to different representation. supported types are: base64, Hex, escape. escape merely outputs NULL bytes as \ 000 and doubles backslashes. in contrast to decode, the string is encoded according to the specified type.
Example: encode (E '2014 \ 000 \ 001', 'base64') = mtizaae =
Function: initcap (string)
Note: Convert the first letter of each word to uppercase and the rest to lowercase. words are sequences of alphanumeric characters separated by non-alphanumeric characters. format all the words in the string. The first letter is in upper case, and the others are in lower case.
Example: initcap ('Hi Thomas ') = hi Thomas
Function: length (string)
Description: number of characters in string indicates the length of a string.
Example: length ('job') = 4
Function: length (stringbytea, encoding name)
Note: number of characters in string in the given encoding. The string must be valid in this encoding. calculates the string length and specifies the encoding of the string.
Example: length ('job', 'utf8') = 4
Function: lpad (string text, length int [, fill text])
Note: fill up the string to length by prepending the characters fill (a space by default ). if the string is already longer than length then it is truncated (on the right ). to the left side of a string, you can automatically fill in the specified string to the left until it reaches the specified length. Multiple automatically filled characters can be specified at the same time.
Example: lpad ('hi', 5, 'xy') = xyxhi
Function: ltrim (string text [, characters text])
Remove: remove the longest string containing only characters from characters (a space by default) from the start of string to delete some characters on the left of the string. You can specify multiple characters to be deleted.
Example: trim
Function: MD5 (string)
Note: calculates the MD5 hash of string, returning the result in hexadecimal encodes the string using MD5.
Example: MD5 ('abc') = 900150983cd24fb0 d6963f7d28e17f72
Function: pg_client_encoding ()
Note: The current client encoding name is encoded by the PG client.
Example: pg_client_encoding () = SQL _ascii
Function: quote_ident (string text)
: Return the given string suitably quoted to be used as an identifier in an SQL statement string. quotes are added only if necessary (I. E ., if the string contains non-identifier characters or wocould be case-folded ). embedded quotes are properly doubled. enclose a string with two quotation marks.
Example: quote_ident ('foo bar') = "foo bar"
Function: quote_literal (string text)
: Return the given string suitably quoted to be used as a string literal in an SQL statement string. embedded Single-quotes and backslashes are properly doubled. single quotation marks are added to both sides of the string. If a single SQL-encoded single quotation mark is displayed in the string, two single quotation marks will be obtained by the table.
Example: quote_literal ('o \ 'Reilly ') = 'O' Reilly'
Function: quote_literal (value anyelement)
Note: coerce the given value to text and then quote it as a literal. embedded Single-quotes and backslashes are properly doubled. converts a value to a string with single quotation marks on both sides. If a single quotation mark appears in the middle of the value, it is expressed as two single quotation marks.
Example: quote_literal (42.5) = '42. 5'
Function: regexp_matches (string text, pattern text [, flags text])
: Return all captured substrings resulting from matching a POSIX regular expression against the string. see section 9.7.3 for more information. matches strings by regular expressions. If yes, the strings are displayed in the result array.
Example: regexp_matches ('foobarbequebaz', '(bar) (beque)') = {bar, beque}
Function: regexp_replace (string text, pattern text, replacement text [, flags text])
Note: replace substring (s) matching a POSIX regular expression. See section 9.7.3 for more information. use regular expressions to replace strings.
Example: regexp_replace ('Thomas ','. [Mn] a. ', 'M') = THM
Function: regexp_split_to_array (string text, pattern text [, flags text])
Description: Split string using a POSIX regular expression as the delimiter. See section 9.7.3 for more information. Use a regular expression to split the string into arrays.
Example: regexp_split_to_array ('Hello world', e' \ s + ') = {Hello, world}
Function: regexp_split_to_table (string text, pattern text [, flags text])
Note: Split string using a POSIX regular expression as the delimiter. See section 9.7.3 for more information. use regular expressions to split strings into tables.
Example: regexp_split_to_table ('Hello world', e' \ s + ') =
Hello
World
(2 rows)
Function: Repeat (string text, number INT)
Note: Repeat string the specified number of times repeats a specified number of times of a string.
Example: Repeat ('pg ', 4) = pgpgpgpg
Function: Replace (string text, from text, to text)
Note: replace all occurrences in string of substring from with substring to replace a substring of a character with another substring.
Example: ('abcdefabcdef ', 'cd', 'xx') = abxxefabxxef
Function: rpad (string text, length int [, fill text])
Note: fill up the string to length by appending the characters fill (a space by default ). if the string is already longer than length then it is truncated. pads The string with the specified string.
Example: rpad ('hi', 5, 'xy') = hixyx
Function: rtrim (string text [, characters text])
Remove: remove the longest string containing only characters from characters (a space by default) from the end of string
Removes the specified character on the right of the string.
Example: rtrim ('trimxxxx', 'x') = trim
Function: split_part (string text, delimiter text, field INT)
Description: Split string on delimiter and return the given field (counting from one) splits the string by the specified sub-string and returns the value at the specified position.
Example: split_part (mailto: & amp; apos; ABC ~ @~ Def ~ @~ Ghi & amp; apos;, mailto: & amp; apos ;~ @~ & Amp; apos;, 2) = def
Function: strpos (string, substring)
Location: Location of specified substring (same as position (substring in string), but note the reversed argument order) specifies the position of the string in the target string
Example: strpos ('high', 'ig ') = 2
Function: substr (string, from [, Count])
Note: extract substring (same as substring (string from for count) Intercepts substrings.
Example: substr ('alphabet ', 3, 2) = Ph
Function: to_ascii (string text [, encoding text])
Note: Convert string to ASCII from another encoding (only supports conversion from Latin1, latin2, latin9, and win1250 encodings) converts a string to an ASCII encoded string.
Example: to_ascii ('karel ') = Karel
Function: to_hex (number Int or bigint)
Note: Convert number to its equivalent hexadecimal representation encodes the value in hexadecimal notation.
Example: to_hex (2147483647) = 7 fffffff
Function: translate (string text, from text, to text)
Note: any character in string that matches a character in the from set is replaced by the corresponding character in the to set replaces some matching characters in the string with the specified string, you can specify multiple target and source characters at the same time.
Example: translate ('20140901', '14', 'ax ') = a23x5
Reprinted from: http://gavin-chen.javaeye.com/blog/262847