2

Is there any familiar function for PATINDEX of mysql for postgresql. I'm trying to make a sql like this in postgres.

SELECT PATINDEX('%schools%', 'W3Schools.com');

which throw an error:

no function matches the given name and argument types. you might need to add explicit type casts

To be more detailed, I'm trying to get seperate number part and string part of a string in Postgresql. I found example like this:

SELECT Section
FROM dbo.Section
ORDER BY LEFT(Section, PATINDEX('%[0-9]%', Section)-1), -- alphabetical sort
         CONVERT(INT, SUBSTRING(Section, PATINDEX('%[0-9]%', Section), LEN(Section))) -- numerical

2 Answers 2

2

There are two ways to implement this, the example as below:

postgres=# select strpos('W3Schools.com','Schools');
 strpos 
--------
      3
(1 row)

postgres=# select position('Schools' in 'W3Schools.com');
 position 
----------
        3
(1 row)

postgres=# select regexp_matches('hahahabc123zzz', '(abc)(123)');
 regexp_matches 
----------------
 {abc,123}

postgres=# select array_to_string(regexp_matches('hahahabc123zzz', '(abc)(123)'),' ');
 array_to_string 
-----------------
 abc 123

postgres=# select (regexp_matches('hahahabc123zzz', '(abc)(123)'))[1] as a, (regexp_matches('hahahabc123zzz', '(abc)(123)'))[2] as b;
  a  |  b  
-----+-----
 abc | 123
(1 row)

Do you want this? And you can get all functions of string process here: https://www.postgresql.org/docs/10/functions-string.html

Sign up to request clarification or add additional context in comments.

6 Comments

I'm sorry but I edited the question. can you please give another answer
@NguyenHoangVu-K11FUGHCM So you want to get a substring?Can you give some example datas?
abc123, I need to seperate 'abc' and '123'. Thanks
do you have a regex for string path? I found one for numeric path (\d+)
"abc123" is just an example ^^ but thanks for your help with regexp_matches. I'm working on it
|
1

Can you try POSITION() function:

SELECT POSITION('schools' in 'W3Schools.com');

2 Comments

I'm sorry but I edited the question. can you please give another answer
@NguyenHoangVu-K11FUGHCM I hope you need regexp_matches()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.