SQL Server: any equivalent of strpos()?
User charindex:
Select CHARINDEX ('S','MICROSOFT SQL SERVER 2000')
Result: 6
Link
Join tables on a partially matched columns
You need to concatenate '%'
with column T2.column
and not with the string value 'T2.column'
:
like ('%' || T2.column)
or
like CONCAT('%', T2.column)
WHERE clause with strpos
"col001,col002"
is a delimited identifier, i.e. the dbms thinks it's a column name. Use single quotes for string literals.
I.e.
select variable, col001, col002
from test
where strpos('col001,col002', variable) > 0
stripos equivalent in MySQL
You can use INSTR(str,substr)
:
Returns the position of the first occurrence of substring
substr
in stringstr
. This is the same as the two-argument form ofLOCATE()
, except that the order of the arguments is reversed.
SELECT INSTR('foobarbar', 'bar');
-> 4
SELECT INSTR('xbar', 'foobar');
-> 0
Difference between STRPOS and POSITION in postgresql
Those functions do the exactly same thing and differ only in syntax. Documentation for strpos()
says:
Location of specified substring (same as position(substring in string), but note the
reversed argument order)
Reason why they both exist and differ only in syntax is that POSITION(str1 IN str2)
is defined by ANSI SQL standard. If PostgreSQL had only strpos()
it wouldn't be able to run ANSI SQL queries and scripts.
Is there any function available to find string position after specified index in postgresql
STRPOS accepts only two arguments. But you can chain a call to substring and easily achieve your objective.
SELECT STRPOS(SUBSTRING('my name is database',2), 'm')
Note that postgresql strings start from 1 and not 0 so you have to put 2 instead of 1 in the above query. Of course the result is relative to the new string 'y name is database' so you will have to add 1 to the result.
What is the CHARINDEX (SQL SERVER) equivalent in POSTGRESQL?
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
Multiple strpos possible?
How about:
if (preg_match('/\{.*?:.*?\}/', $string)) {
echo 'string is good.';
}
Where:
/
\{ : openning curly brace (must be escaped, it' a special char in regex)
.*? : 0 or more any char (non greeddy)
: : semicolon
.*? : 0 or more any char (non greeddy)
\} : closing curly brace (must be escaped, it' a special char in regex)
/
Related Topics
Use a Like Clause in Part of an Inner Join
Insert into a Row at Specific Position into SQL Server Table with Pk
Row with Minimum Value of a Column
Using Different Order by with Union
SQL - Pivot Table and Group by Not Working
MySQL Join/Group_Concat Second Table
How to Pass a Parameter to a T-SQL Script
Best Practices for the Order of Joined Columns in a SQL Join
Go with SQL Server Driver Is Unable to Connect Successfully, Login Fail
There Is Already an Object Named '#Result' in the Database
Select Records in on Table Based on Conditions from Another Table
T:Sql: Select Values from Rows as Columns
SQL Listing All Column Names Alphabetically
What Is the Equivalent of Regexp_Substr in MySQL
Using Guid in SQLite Select Where Guid Is Stored in the SQLite Db as Binaries
Merge Overlapping Time Intervals, How
Ansi SQL Version of Select Top 1
Retrieving Column and Other Metadata Information in Teradata