In PostgreSQL, the equivalent function to SUBSTR() is also SUBSTRING(). The SUBSTRING() function is used to extract a substring from a string based on a specified starting position and length.
Here's the syntax for the SUBSTRING() function in PostgreSQL:
SUBSTRING(string FROM start FOR length)
- string: The string from which the substring will be extracted.
- start: The starting position from which to extract the substring. It is 1-based (the first character in the string has a position of 1).
- length: The length of the substring to extract.
Let's see an example to illustrate how to use the SUBSTRING() function in PostgreSQL:
Suppose we have a string 'Hello, World!' and we want to extract a substring starting from the 7th position and extending to the end of the string:
SELECT SUBSTRING('Hello, World!' FROM 7) AS extracted_string;
This query will return 'World!' as the extracted substring.
Now, let's say we want to extract only the first 5 characters from the string:
SELECT SUBSTRING('Hello, World!' FROM 1 FOR 5) AS extracted_string;
This query will return 'Hello' as the extracted substring.
So, the SUBSTRING() function in PostgreSQL allows you to extract substrings from strings based on specified starting positions and lengths.
No comments:
Post a Comment