Faizal
Faizal

Reputation: 1345

Postgresql : Count columns whose value starting with the value of another column

How do I count rows where a column value starts with a another column value ?

For example, I have table products shown below

---------------------------
id  code    abbreviation
---------------------------
1   AA01    AA
2   AB02    AB
3   AA03    AA
4   AA04    AB
---------------------------

I want to get the count of products whose code starts with abbreviation. A query like this

select count(*) from products where code ilike abbreviation+'%'

I am using postgresql 9.5.3

Upvotes: 0

Views: 1107

Answers (2)

Stefan
Stefan

Reputation: 146

The string concatenation operator in postgresql is: ||

select count(*) from products where code like abbreviation || '%';

Upvotes: 4

Kumar_Vikas
Kumar_Vikas

Reputation: 845

You can try:

select count(*) from products where code like '%'+abbreviation+'%'

But i am not sure why do you need this type of query.

Upvotes: -1

Related Questions