1

i'm using postgres and i want tried date_diff, but i get this error

Warning: pg_query(): Query failed: ERROR: operator does not exist: boolean = integer LINE 1

and then this is my query

select id_mitra
      ,company_name 
from ref_mitra
where reject=0 and DATE_DIFF(now(),date_status) > 3 and active='N'

what wrong in my query?

5
  • Show your table description please. Maybe reject is a boolean. Commented May 11, 2015 at 5:41
  • If you want to use the value 0 (zero), you have to cast this value to a boolean: reject= CAST(0 as boolean) Commented May 11, 2015 at 5:43
  • You need to compare a boolean to a boolean where reject = false. There is also no date_diff function in Postgres. Commented May 11, 2015 at 5:44
  • @Wida try reject=0::boolean or reject=false Commented May 11, 2015 at 5:44
  • that table is save date, like this 2014-09-26 Commented May 11, 2015 at 5:45

2 Answers 2

2

You have two errors in your query:

  1. you are comparing a number (0) to a boolean
  2. there is no date_diff() function in Postgres

Assuming date_status is a (real) date column, your query should look like this:

select id_mitra
      ,company_name 
from ref_mitra
where reject = false 
  and current_date - date_status > 3 
  and active='N';

now() returns a timestamp but as you apparently want the number of days (not an interval) you should use current_date instead of now().

I am not sure what date_diff() really does, maybe you need to write date_status - current_date to get the same behaviour.

Instead of reject = false you can also use where not reject

Sign up to request clarification or add additional context in comments.

2 Comments

this is what I was going to answer :)
Using NOT for a boolean is a nice trick, but only when NOT isn't a query parameter: You can't use a prepared statement where NOT depends on user input.
1

Cast your value 0 to boolean:

select id_mitra
      ,company_name 
from ref_mitra
where reject = CAST(0 as boolean) 
and DATE_DIFF(now(),date_status) > 3 
and active='N';

Or use a boolean:

select id_mitra
      ,company_name 
from ref_mitra
where reject = false 
and DATE_DIFF(now(),date_status) > 3 
and active='N';

DATE_DIFF() isn't a standard function in PostgreSQL, but maybe you created a function using this name.

2 Comments

Are you sure that date_diff is avail. in PostgreSQL?
@vivek: No I'm not, but you could create it yourself using CREATE FUNCTION

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.