Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LIKE with % on column names

Here is my query that results in a syntax error:

SELECT *  FROM account_invoice,sale_order WHERE sale_order.name LIKE %account_invoice.origin% 

The account_invoice.origin field contains the text of sale_order.name, plus other text as well, so I need to match sale_order.name string anywhere in the account_invoice.origin string.

I'm using PostgreSQL 8.4.

like image 548
user1806801 Avatar asked Nov 07 '12 17:11

user1806801


People also ask

Which special characters are allowed in column names?

The at sign, dollar sign ($), number sign, or underscore.

How can I mention column name in SQL?

In SQL Server, we can specify the column name with space in square bracket or parenthesis.

Can I use SQL keyword as column name?

In SQL, certain words are reserved. These are called Keywords or Reserved Words. These words cannot be used as identifiers i.e. as column names in SQL.


1 Answers

Try this

SELECT *  FROM account_invoice,sale_order WHERE sale_order.name LIKE '%'  || account_invoice.origin || '%' 

% needs single quote because the pattern is a string.

|| is the operator for concatenation.

like image 97
Marc Avatar answered Sep 21 '22 21:09

Marc