Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare text values in PostgreSQL

Say, there are two tables. One contains a field with long text values (for example foobarbaz), the other one contains shorter values (foobar and someothertext). I'd like to retrieve values from two tables with the following condition: the text must not be equal, but the beginning of the long string must match the short string. Is there a (neat) way to do this in Postgres? Thanks in advance.

like image 730
John Doe Avatar asked Jun 14 '12 10:06

John Doe


People also ask

What is || in PostgreSQL?

The PostgreSQL concatenate operator ( || ) is used to concatenate two or more strings and non strings.

How do I split text in PostgreSQL?

The PostgreSQL SPLIT_PART() function is used to split a string from a specific delimiter and these queries return the nth substring. Let's analyze the above syntax: The string argument is the string to be split. The delimiter is a string used as the delimiter for splitting.

How do you check if a string contains a substring PostgreSQL?

Use the substring() Function to SELECT if String Contains a Substring Match in PostgreSQL. The substring() returns the strings similar to abc in our case or contains abc . We then match the returned results to the str using the ~~ operator, short for like , and if they match, we select the results from the table.


1 Answers

How about:

SELECT <whatever>
  FROM <your tables>
 WHERE one_field <> the_other_field
   AND position(the_other_field in one_field) = 1;

See string functions and operators.

like image 189
dslh Avatar answered Oct 11 '22 00:10

dslh