0

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

2
  • Are the abbreviations in fixed size? Commented Nov 14, 2016 at 8:20
  • @Dudu Markovitz No, abbreviations can be of any length Commented Nov 14, 2016 at 12:56

2 Answers 2

4

The string concatenation operator in postgresql is: ||

select count(*) from products where code like abbreviation || '%';
Sign up to request clarification or add additional context in comments.

Comments

-1

You can try:

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.