String functions for T-SQL
This topic provides reference information about string function compatibility between Microsoft SQL Server 2019 and Amazon Aurora PostgreSQL. You can gain insights into how various string functions in SQL Server map to their PostgreSQL equivalents, which is crucial for database migration projects. The topic highlights supported functions, unsupported ones, and alternative approaches in PostgreSQL.
Feature compatibility | AWS SCT / AWS DMS automation level | AWS SCT action code index | Key differences |
---|---|---|---|
|
|
N/A |
Syntax and option differences. |
SQL Server Usage
String functions are typically scalar functions that perform an operation on string input and return a string or a numeric value.
Syntax and Examples
The following table includes the most commonly used string functions.
Function | Purpose | Example | Result | Comments |
---|---|---|---|---|
|
Convert an ASCII or UNICODE character to its ASCII or UNICODE code. |
|
65 |
Returns a numeric integer value. |
|
Convert between ASCII or UNICODE code to a string character. |
|
'A' |
Numeric integer value as input. |
|
Find the starting position of one string expression or string pattern within another string expression. |
|
2 |
Returns a numeric integer value. |
|
Combine multiple string input expressions into a single string with, or without, a separator character (WS). |
|
'ab', 'a,b' |
|
|
Return a partial string from another string expression based on position and length. |
|
'ab', 'bc' |
|
|
Return a string with all characters in lower or upper case. Use for presentation or to handle case insensitive expressions. |
|
'abcd' |
|
|
Remove leading and trailing spaces. |
|
'abc d ' |
|
|
Convert a numeric value to a string. |
|
3.142 |
Numeric expressions as input. |
|
Return a string in reverse order. |
|
'dcba' |
|
|
Return a string that consists of zero or more concatenated copies of another string expression. |
|
'abcabcabc' |
|
|
Replace all occurrences of a string expression with another. |
|
'axyd' |
|
|
Parse a list of values with a separator and return a set of all individual elements. |
|
12 |
|
|
Return a string that consists of concatenated string values in row groups. |
|
1 'ab' |
2 'c' |
For more information, see String Functions (Transact-SQL)
PostgreSQL Usage
Most of SQL Server string functions are supported in PostgreSQL, there are few which aren’t:
-
UNICODE
returns the integer value of the first character as defined by the Unicode standard. If you will use UTF8 input, ASCII can be used to get the same results. -
PATINDEX
returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern isn’t found, there is no equivalent function for that but you can create the same function with the same name so it will be fully compatible.
Some of the functions aren’t supported but they have an equivalent function in PostgreSQL that you can use to get the same functionality.
Some of the functions such as regular expressions don’t exist in SQL Server and may be useful for your application.
Syntax and Examples
The following table includes the most commonly used string functions.
PostgreSQL function | Function definition |
---|---|
|
Concatenate the text representations of all the arguments: |
|
Returns char, with all letters lowercase or uppercase: |
|
Returns |
|
Replace substrings matching a POSIX regular expression: |
|
Return all captured substrings resulting from matching a POSIX regular expression against the string: REGEXP_MATCHES ('http://www.aws.com/products', '(http://[[: alnum:]]+.*/)') The result is SUBSTRING ('http://www.aws.com/products', '(http://[[: alnum:]]+.*/)') The result is |
|
Returns char with every occurrence of search string replaced with a replacement string: |
|
Remove the longest string containing only characters from characters (a space by default) from the start of string: |
|
Extract substring: |
|
Remove the longest string containing only characters from characters (a space by default) from the start, end, or both ends: |
|
Returns the decimal representation in the database character set of the first character of char: |
|
Return the length of char: |
To create the PATINDEX
function, use the following code snippet. Note the 0 means that the expression doesn’t exist so the first position will be 1.
CREATE OR REPLACE FUNCTION "patindex"( "pattern" VARCHAR, "expression" VARCHAR ) RETURNS INT AS $BODY$ SELECT COALESCE(STRPOS($2,( SELECT(REGEXP_MATCHES($2,'(' || REPLACE( REPLACE(TRIM( $1, '%' ), '%', '.*?' ), '_', '.' ) || ')','i') )[ 1 ] LIMIT 1)),0); $BODY$ LANGUAGE 'sql' IMMUTABLE; SELECT patindex( 'Lo%', 'Long String' ); patindex 1 SELECT patindex( '%rin%', 'Long String' ); patindex 8 SELECT patindex( '%g_S%', 'Long String' ); patindex 4
Summary
SQL Server function | Aurora PostgreSQL function |
---|---|
|
|
|
For UTF8 inputs, you can use only |
|
|
|
|
|
See examples |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For more information, see String Functions and Operators