31

I am trying CHARINDEX in Postgresql. But it says:

function CHARINDEX() does not exist

If no such inbuilt function exists in postgresql, then is there any function that serves as an alternative to charindex?
If yes, What is the CHARINDEX (SQL SERVER) equivalent in POSTGRESQL ?

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Manoj Soundararajan
  • 371
  • 1
  • 3
  • 13
  • 2
    What is it supposed to do? http://www.postgresql.org/docs/9.4/static/functions-string.html (probably you need `position(...)` ) – joop Jun 11 '15 at 10:38

2 Answers2

55

The equivalent function in postgresql is:

strpos(string, substring)

Or:

position(substring in string)

They are equivalent, just with different order in parameters.
If you also need parameter start_location, you will need to pass a substring to strpos.

You can find them in: https://www.postgresql.org/docs/current/functions-string.html

user_0
  • 3,173
  • 20
  • 33
0
  • In SQL Server CHARINDEX syntax is

    SELECT CHARINDEX(Substring, String)
    

    and example as like

    SELECT CHARINDEX('m','Zarm')
    

    this query returned 4.

  • Case of PostgreSQL syntax is

    strpos(string, substring)
    

    And example is

    SELECT strpos('Amal', 'l')
    

    this query returned 4.

cursorrux
  • 1,382
  • 4
  • 9
  • 20