Definition of PostgreSQL SPLIT_PART()
PostgreSQL split_part function is used to split a string into nth substring by using a specified delimiter; the string’s splitting is based on a specified delimiter that we have used. Splitting of the string using the split_part function is starting from left to right; it will always splitting the string from left to right.PostgreSQL split_part function requires three arguments as string, delimiter and position. The string argument states that string which has used to split, a delimiter is used to split the string into the nth part. The position states which position string we want to return.
Syntax:
Below is the syntax of the split_part function in PostgreSQL.
Split_part (String, delimiter, Position)
Select split_part (String (In this position column name as we have defined a string.), delimiter, Position), split_part (String (In this position column name as we have defined a string.), delimiter, Position), Column_nameN from table_name;
Parameter:
Below is the parameter description of the above syntax.
- String: String is states that which string we have used to split using a split_part function in PostgreSQL. We can use any of the string to split it; we can also use a column name as a substring to split the data from the column.
- Split_part (): PostgreSQL split_part function is used to split string into specified delimiter and return into result as an nth substring, the splitting of string is based on a specified delimiter which we have used.
- Delimiter: Delimiter is used to split the string into sub-parts by using a split_part function in PostgreSQL. We can split the string into a number of parts using a delimiter.
- Position: Position in split part function states which position string we want to return using split_part function in PostgreSQL. The position will start from 1, and it should not be negative; it is always a positive integer.
- Select: Select is used to select a column of the table using the split_part function in PostgreSQL. We can select a column using the split_part function and split column string.
- Column 1 to Column N: Column name used to get the information from the table. We can split the column data rows using the split_part function in PostgreSQL.
- Table name: Table name is used to retrieve data from the specified column using the split_part function in PostgreSQL.
How PostgreSQL SPLIT_PART() Function Works?
Below is the working of the PostgreSQL split_part function.
- Split part function is very important and useful in PostgreSQL to split a string into nth parts.
- We need to define a position value as a positive number; a negative value is not allowed in the position argument.
- We need to define a greater than zero value at the time using the split_part function in PostgreSQL.
- In the below first example, we have used a position of 1; after using position 1, it will display the string’s value is X.
- Using value as 0 and -1 will display the error as “ERROR: field position must be greater than zero”.
SELECT SPLIT_PART('X,Y,Z', ',', 1);
SELECT SPLIT_PART('X,Y,Z', ',', -1);
SELECT SPLIT_PART('X,Y,Z', ',', 0);
- It is used to split string into specified delimiter and return into result as nth substring, the splitting of string is based on a specified delimiter which we have used.
- The string argument states which string we have used to split using a split_part function in PostgreSQL. We can use any of the string to split it; we can also use a column name as a substring to split the data from the column.
- Delimiter argument is used to split the string into sub-parts by using a split_part function in PostgreSQL. We can split the string into a number of parts using delimiter.
- The position argument is split part function states which position string we want to return using split_part function. The position will start from 1 and it should not be negative; it is always positive integer.
- Splitting of the string using the split_part function is starting from left to right; it will always splitting the string from left to right.
- It requires three arguments as string, delimiter and position.
Example to Implement SPLIT_PART () Function in PostgreSQL
- Below is the example of split_part function in PostgreSQL is as follows.
- We have using example of the employee table to describe the example of the split_part function in PostgreSQL. Below is the employee table are as follows.
select * from employee;
1. In the below example we have split ABBCCD string into three parts using a delimiter. In the first part we have split the string as AB in the second part split as BC, and in the third part we split as CD.
SELECT SPLIT_PART('AB,BC,CD', ',', 1);
SELECT SPLIT_PART('AB,BC,CD', ',', 2);
SELECT SPLIT_PART('AB,BC,CD', ',', 3);
2. In the below example, we have split the date of joining column as year, month and date wise into three columns.
SELECT emp_id, emp_name, emp_phone, emp_salary, split_part(date_of_joining::TEXT,'-',
1) AS Year, split_part(date_of_joining::TEXT,'-', 2) AS Month, split_part(date_of_joining::TEXT,'-', 3) AS Date FROM employee;
In the above example, we have split the joining column’s date into three substring as date, year and month wise.
3. Below example shows that only display the month and year of employee joining from employee table.
SELECT emp_id, emp_name, emp_phone, emp_salary, split_part(date_of_joining::TEXT,'-',
1) AS Year, split_part(date_of_joining::TEXT,'-', 2) AS Month FROM employee;
Advantages of Using SPLIT_PART() Functions in PostgreSQL
- The split part function is used to split the string into nth part in PostgreSQL.
- By using a split_part function we can identify common data from the table column.
- We can increase the quality of data by using split_part function in PostgreSQL.
- We can extract multiple substrings from one string in PostgreSQL.
- Split_part function is essential and useful in PostgreSQL.
Conclusion
It is used to split string by using specified delimiter and return into result as nth substring, the splitting of string is based on a specified delimiter which we have used. Split_part function requires three arguments as string, delimiter and position.
Recommended Articles
This is a guide to PostgreSQL SPLIT_PART(). Here we also discuss the Introduction and how postgresql split_part() function works? Along with different examples and code implementation. You may also have a look at the following articles to learn more –